Creating numeric variable from an addition

Hello,

I am trying to write a template that has a choice that updates an ‘amount’ member. Here is what I have:

template RawProduct
      with
        producer : Party
        amount : Amount
...
      where
        signatory producer
        controller producer can
          AddProductionRecord : (ContractId RawProduct, ContractId ProductionRecord)
            with
              productionRecord : ProductionRecord
            do
              assert (productionRecord.productInfo.typeId == productType)

              productionRecordCid <- create productionRecord
              newQuantity <- amount.quantity + productionRecord.productInfo.amount.quantity
              ...

At that final line from the excerpt, I am getting “Couldn’t match type ‘Numeric 10’ with ‘Update Decimal’
arising from a functional dependency between:…”

How can I fix this?

N.B. ProductionRecord is also a template

Thanks!

To clarify; I am passing the template as choice argument to saving specifying all the fields again!

To create an ordinary variable in this do, use let newQuantity = instead of newQuantity <-. In terms of the recent guide, <- “unpacks” the computation, but here there is nothing to unpack.

It would be unwise to automatically discriminate between these two cases for two reasons:

  1. ... <- create productionRecord is still meaningful, regardless of whether you use the productionRecordCid variable afterwards. However, if you never use newQuantity, you can simply eliminate the binding, because you know it is useless if the variable is unused.
  2. It is perfectly meaningful to use let to store Updates, for multiple Update layers to exist, and for it to be unknown whether an expression produces an Update or not. Changing the program in this case would yield “jittery” code, with behavior “snapping” back and forth depending on code elsewhere in the program changing. That would have…bad effects on understanding, to say the least.
1 Like

Hi @alex_m,
that last line is a pure computation, put you need it to be an Update. You can replace it with let newQuantity = amoun.quantity + ... or wrap it in a pure like

newQuantity <- pure $ amount.quantity + productionRecord.productInfo.amount.quantity

The error message is a bit confusing because type Numeric 10 = Decimal. So the compiler actually tells you that it expected an Update Decimal instead of a Decimal.

3 Likes