Arithmetic action on two variables

Hello, everyone

Let’s say I have:

Type Percent = Numeric 2

benePercent1 : Percent
totalPrimaryBene : Percent

Check isPrimary as well

I would like to do something like that:
totalPrimaryBene = if(isPrimary) then totalPrimaryBene + benePercent1 else benePercent1

However, I receive an error. Is there a way for me to fix this?

1 Like

Variables in Daml are immutable so you cannot change the value of totalPrimaryBene. But you can define a new variable:

let totalPrimaryBene' = if isPrimary then totalPrimaryBene + benePercent1 else benePercent2

In some cases, you can reuse the variable name and shadow the existing variable name. Note that this is still very different from mutation: You are still defining a new variable. You are just hiding the existing one within the scope you’re currently in. One case where that works is within a do block:

do totalPrimaryBene <- pure (if isPrimary then totalPrimaryBene + benePercent1 else benePercent2)
     …
1 Like

@cocreature
Thank you so much , Moritz.