Unserializable Result Type

Hello good morning. I would like to ask if there’s a way to print out unserializable result types? For my instance it’s an “unserializable scenario result”.
Thanks!

2 Likes

Hi @carleslabon!

Implementing or deriving Show typeclass instance for your type will allow printing values of this type. Not sure if that is what you are asking?

I will be able to help more if you provide the error message and DAML code that causes this error.

See docs:

2 Likes

Hello @Leonid_Shlyapnikov, below are the following:

Error message: “Unserializable scenario result”.

DAML code:


import DA.Optional

template Sample
  with
    p : Party
  where
    signatory p

    controller p can
      nonconsuming SampleConsume : ()
        with x : Int
             cid : ContractId Sample
        do
          if x > 0
            then archive cid
            else pure ()

whenSome_test = scenario do
  p <- getParty "p"
  cid <- submit p (create Sample with p)
  let 
    x = whenSome None (\x -> submit p $ exercise cid $ SampleConsume x cid)
  return(x)
1 Like

Hi @carleslabon,
In your example, x has type Scenario () which is not serializable. The definition of serializability is built into DAML so you cannot change that.

However, I expect this example is not what you want: You are only defining a scenario and calling it x, you are not actually executing it. For that, you need to sequence it with <- within the do block:

whenSome_test = scenario do
  p <- getParty "p"
  cid <- submit p (create Sample with p)
  x <- whenSome None (\x -> submit p $ exercise cid $ SampleConsume x cid)
  return(x)

Then x has type () instead of Scenario () which is serializable and things work out fine.

4 Likes

@carleslabon!

What version of DAML SDK are you using in your project? I tried your example, it worked on SDK 1.4.0:

$ daml version
DAML SDK versions:
  1.4.0                                    (project SDK version from daml.yaml)

Here is the output:
Screenshot from 2020-09-14 09-40-04

2 Likes

@Leonid_Shlyapnikov it’s not a fatal error, it’s just a message you see in the transaction view (but not the table view in your screenshot) at the end since the result type cannot be printed.

2 Likes

Got it.

What @cocreature said… Replace

let 
    x = whenSome None (\x -> submit p $ exercise 

where x has type Scenario ()

with

x <- whenSome None (\x -> submit p $ exercise cid $ SampleConsume x cid)

where x has type () because of the “unwrapping” nature of flat map operator: <-.

In other words, see the same two DAML expressions with types:

let 
    x : Scenario () = whenSome None (\x -> submit p $ exercise cid $ SampleConsume x cid)

see x: Scenario () = ...
and

x : () <- whenSome None (\x -> submit p $ exercise cid $ SampleConsume x cid)

I personally like adding types to more complex expressions, it might help with the code readability.

3 Likes

Hello @Leonid_Shlyapnikov, I tried it with the flat map operator “<-” and I finally got the result as expected. Thanks!

1 Like