Conditionally consuming choice

Can DAML allow for a choice to be consuming based on a condition in the body of the choice?

(I am aware that this is not supported but I am more curious if it is just a missing feature or there is a deeper reason behind having to declare a choice consuming (or not) a priori.

Consider this example:

template Foo
  with
    party : Party
  where
    signatory party

    controller party can
      MaybeConsume : ContractId Foo
        with
          conditionToConsume : Bool
        do
          if conditionToConsume
          then create this  -- consume and recreate
          else return self  -- don't consume
3 Likes

Are you specifically looking for something that has the same privacy implications as a consuming choice? If not, you can use a nonconsuming choice + archive self, e.g., taking your example:

template Foo
  with
    party : Party
  where
    signatory party

    nonconsuming choice MaybeConsume : ContractId Foo
        with
          conditionToConsume : Bool
        controller party
       do if conditionToConsume
              then do archive self
                      create this
              else pure self

5 Likes

nice! hadn’t thought of that - thank you. What do you mean by same privacy implications?

A non-consuming exercise is only visible to signatories and the actor. A consuming exercise on the other hand is visible to all stakeholders and actors so it is also visible to observers. IME, the behavior of non-consuming choices is often what you actually want so it’s not that uncommon to use a non-consuming choice and archive self to get this. You can also use preconsuming and postconsuming choices which desugar to a non-consuming choice with the archive self at the beginning or end.

1 Like

Just a note that, should the choice archive the contract, observers on that contract still see the archive itself, though they don’t see the non-consuming choice execution or its other consequences.

This is all documented here.

3 Likes