I see code in the docs written in both ways, is there a difference or a canonical way to do write this?
Example:
controller owner can
UpdateTelephone
: ContractId Contact
with
newTelephone : Text
do
create this with
telephone = newTelephone
vs.
choice Sign : ContractId Pending with
signer : Party
controller signer
do
-- Check the controller is in the toSign list, and if they are, sign the Pending contract
assert (signer `elem` toSign)
create this with alreadySigned = signer :: alreadySigned
Hi @anthony, there are two differences:
- The
controller x can
syntax implicitly adds x
to the list of observers. For the second syntax, you have to add x
to the list of observers separately.
- The second form is more general. Specifically, the controller expression can refer to the fields of your choice argument. You make use of that in your example above: The controller
signer
is only introduced by the choice argument. With the controller … can
syntax, the controller expression can only refer to the fields of the template so you will get an error if you try to rephrase your second example using the controller … can
syntax. You can find more information on that at https://blog.daml.com/daml-driven/daml-does-yoga-an-introduction-to-flexible-controllers
I hope that helps!
3 Likes
So for the second difference controller ... can
cannot accept new arguments/fields but choice ... controller
can?
Also:
You can have fields in the choice argument with both syntaxes. What you cannot do is refer to the field in the controller expression. Here are 3 examples that hopefully make it clear:
template T
with
p0 : Party
where
signatory p0
-- This is ok, the controller expression p0 does not refer to the choice argument
controller p0 can
A : ()
with
p1 : Party
do pure ()
-- This is ok, the controller expression does refer to the choice argument but we use syntax 2
choice B : ()
with
p1 : Party
controller p1
do pure ()
-- This is an error, the controller expression p1 refers to the choice argument but we use syntax 1
controller p1 can
C : ()
with
p1 : Party
do pure ()
```.
1 Like