Utilizing Either As Choice Return Type
- akhanOP 4
Hey folks,
I want to use the Either module to return one of two potential contract types in a template choice. I’m having issues figuring out the correct syntax to use.
Code sample and a screenshot of the error are below:
template ContractCreation with owner: Party where signatory owner controller owner can nonconsuming CreateContract: Either (ContractId Contract1) (ContractId Contract2) with creatorType: Text do if (creatorType == "1") then do create Contract1 with owner else if (creatorType == "2") then do create Contract2 with owner else abort "Error" template Contract1 with owner: Party where signatory owner template Contract2 with owner: Party where signatory owner - cocreature 3
DAML doesn’t have any concept of implicit conversions. In your example,
creategives you back aUpdate (ContractId Contract2)but you need anUpdate (Either (ContractId Contract1) (Contract2))which is why you see the error.To go from
atoEither a byou have to use theLeftconstructor and similarly theRightconstructor to go frombtoEither a b.Putting this to use in your example, you get something like the following:
module Main where template ContractCreation with owner: Party where signatory owner controller owner can nonconsuming CreateContract: Either (ContractId Contract1) (ContractId Contract2) with creatorType: Text do if (creatorType == "1") then do cid <- create Contract1 with owner pure (Left cid) else if (creatorType == "2") then do cid <- create Contract2 with owner pure (Right cid) else abort "Error" template Contract1 with owner: Party where signatory owner template Contract2 with owner: Party where signatory ownerYou could also use either of the following two in place of the do:
fmap Left (create Contract1 with owner) Left <$> create Contract1 with ownerwhich one you prefer is mostly a matter of taste. The
doversion can often be easier to figure out as you get started but over time the more concise version usingfmapor the infix version<$>often becomes appealing. - Gary_Verhaegen 8
Adding a bit of context to make the last two lines less magical. First off,
<$>is the same function asfmap, but defined as an infix operator. This meansfmap a bis exactly the same asa <$> b. In the following I’ll talk only aboutfmap, but obviously this applies just as well to<$>.fmapis a function defined on theFunctortype class. As an early, informal, and incomplete explanation, you can think of a functor as some sort of container, andfmap function boxas applying a functionfunctionto whatever is currently in the containerbox, returning a new box containing the result. The details of how that works depend on the type of the container. Here are a few concrete examples:$ daml repl daml> succ 1 2 daml> succ 4 5 daml> fmap succ [1] [2] daml> fmap succ [1, 2, 3, 4] [2,3,4,5] daml> fmap succ (Some 4) Some 5 daml> fmap succ (None : Optional Int) None daml>As you can see, using
fmap fon aListwill return a new List of the same length where each element is the result of applyingfto the corresponding element in the original list. Usingfmap fon anOptionalresults in a newOptionalthat isNone(empty) if the original wasNone, and aSomewith the value of applying theffunction if there was originally a value.These could be defined as:
fmap : (a -> b) -> [a] -> [b] fmap f [] = [] fmap f (x::xs) = f x :: fmap f xsand
fmap : (a -> b) -> Optional a -> Optional b fmap f None = None fmap f (Some x) = Some (f x)(These are not quite the correct syntax as
fmapis a typeclass method, not a function. See this page for an example of how to implement typeclass instances.)Stretching the analogy a bit, you can also think of
Updateas a container of sorts: it’s a magic box that will contain a result once the code has been run on the ledger. Despite the analogy not holding out too well here (it was informal and incomplete to begin with),Updateis also an instance ofFunctor, and you can similarly think offmapas somehow sending the function inside the update to be applied there. How would you go about doing that? Well, first you would have to create a context in which you can run some code (do), then in that context you can extract the value from the existing update (v <- update), and then you can apply the function and wrap the result in a newUpdatecontext (pure (f v)). So in this case the implementation would look something like:fmap : (a -> b) -> Update a -> Update b fmap f u = do v <- u pure (f v)which is pretty much exactly the code in the branches of your choice, where
Left(andRightfor the other branch) is the function you want to apply.So the final code using
fmapwould be:module Main where template ContractCreation with owner: Party where signatory owner controller owner can nonconsuming CreateContract: Either (ContractId Contract1) (ContractId Contract2) with creatorType: Text do if (creatorType == "1") then fmap Left (create Contract1 with owner) else if (creatorType == "2") then fmap Right (create Contract2 with owner) else abort "Error" template Contract1 with owner: Party where signatory owner template Contract2 with owner: Party where signatory ownerAs a final note, when I have a three-way (or more) branch, I tend to prefer pattern matching to if-then-else. This is very much a personal preference and there’s nothing wrong with if-then-else, but in case you’re curious here is what the body of the choice would look like using pattern matching (i.e. a
case ... ofexpression):do case creatorType of "1" -> fmap Left (create Contract1 with owner) "2" -> fmap Right (create Contract2 with owner) _ -> abort "Error" - akhan 4
Thank you @cocreature. Marked as the answer, I appreciate the clarity on the syntax.
@Gary_Verhaegen, awesome awesome response and explanation. Really appreciated that extra level of explanation.
