Choice - Return type

Hi All,

I understand that, “create” gives back an update for the contract. In the below example, splitCid & restCid are being returned. But wanted to know where are this return types being utilized ?

      -- Split the IOU by dividing the amount.
-- BEGIN_IOU_TEMPLATE_SPLIT
    choice Iou_Split : (IouCid, IouCid)
        with
        splitAmount: Decimal
      controller owner
      do
        let restAmount = amount - splitAmount
        splitCid <- create this with amount = splitAmount
        restCid <- create this with amount = restAmount
        return (splitCid, restCid)
-- END_IOU_TEMPLATE_SPLIT

Thank you!

@prajwalhegde3
The return type declaration in the choice signature is required because Daml enforces strict type checking. In your example Iou_Split choice returns Update (IouCid, IouCid). In other words it returns an Update action (as choices always return an Update), which results in a tuple with 2 IouCid. The latter, I suppose, is a custom data type most likely representing ContractId Iou.
As for where you can use the return values of a choice, you can use them for example in a choice in another template:

        (mySplitCid, myRestCid) <- exercise iouCid Iou_Split 
          with splitAmount = 10.0

Because the return type of a choice is declared in the choice signature, the compiler knows that the types of mySplitCid and myRestCid variables is IouCid.
Does this answer your question? Or have I misunderstood what you’re asking?

1 Like

@a_putkov demonstrates where choice return value can be used within Daml. The value can also be accessed outside Daml. For example, when you use exercise from TypeScript you get the result back as a decoded value. You can also access it when reading transactions over the gRPC ledger API.

1 Like

Thank you for the explanation @a_putkov @Stephen