Iterating over a list with `createCmd` in a script
I often write the following, intuitively thinking that it should work:
forA_ list $ submit party do createCmd
This throws the error:
Couldn't match type ‘Commands (ContractId t0)’ with ‘Script b0’
Expected type: MyListTemplate -> Script b0
Actual type: MyListTemplate
-> Commands (ContractId t0)
I’ve meanwhile found out what it should be, and I think I understood why that is:
submit party do forA_ list createCmd
But could someone ELI5 (explain like I’m 5) why the former approach doesn’t work?
EDIT:
I suppose the confusing thing is that this actually does work:
forA_ list (\c -> submit party do createCmd c)
So why is submit party do createCmd not the same as \c -> submit party do createCmd c in this case?
forA_ list $ submit party . createCmd should also work. Your example is equivalent to forA_ list $ \c -> ((submit party) createCmd) c which leads to the type error you are seeing:
submit has type Party -> Commands a -> Script a but the second argument you are applying it to is createCmd not createCmd c which has the wrong type.
submit party $ do forA_ list createCmd has different semantics. It creates all contracts in a single transaction rather than spread out over multiple transactions.
Great answer, and truly ELI5
thanks!