Using Either to toggle between types based on data restrictions
- Stephen_HerzigOP
I have a question about the use of ‘Either’ when I need to have the option to ‘toggle’ between different Types in the event that a signatory would want to have the same basic data available for a ‘result’ but may want to restrict some of that data with a Bool (see ‘restricted’ below in the template). Basically, the ‘RestrictedResult’ would just provide less data if it is ‘restricted’.
How would I select either type ‘Result’ vs. ‘RestrictedResult’ as what I am trying isn’t working.
template ApiCall with provider: Party requester: Party broker: Party idService: Party entity: Text entityId: Text restricted: Bool requested: Time where signatory broker, requester, idService observer provider choice Answer: ContractId EsgResult with result : Either Result RestrictedResult result = if (restricted = False) then do result: Result else result: RestrictedResult controller provider do asOf <- getTime -- add result here create EsgResult with .. - WallaceKelly 1
Does this get you closer to your intention?
template ApiCall with provider: Party requester: Party broker: Party idService: Party entity: Text entityId: Text restricted: Bool requested: Time where signatory broker, requester, idService observer provider choice Answer: ContractId EsgResult controller provider do let result = if restricted then Right $ RestrictedResult with.. else Left $ Result with.. asOf <- getTime create EsgResult with .. - a_putkov 2
@Stephen_Herzig
I hope you find the example provided by @WallaceKelly useful.
To add to @WallaceKelly’s response, one error to note in your code, which @WallaceKelly corrected, is that the value of the typeEithermust be eitherRight someValueorLeft someOtherValue.
The other problem in your code is that, if you’d like to pass the value of theresultvariable as an argument to theAnswerchoice, then the logic that assigns the value to theresultmust be implemented outside of theAnswerchoice. In other words the if statement, where you choose the value of theresultvariable as eitherRight $ RestrictedResult with ..orLeft $ Result with ..cannot be implemented inside theAnswerchoice. It must be implemented elsewhere.
Alternatively you can have the logic that assigns the value to theresultinside theAnswerchoice, as in @WallaceKelly’s example. But then you cannot pass the value of theresultas an argument to theAnswerchoice. - asarpeshkar 2
It’s important to note that
Either Result RestrictedResultisn’t “toggling” betweenResultandRestrictedResult–it’s just a regular type like any other type. It doesn’t bypass the requirement that every expression has a single type, which is whyif (restricted = False) then do result: Result else result: RestrictedResultdoesn’t work, the
if-expression can’t have the typeResultand the typeRestrictedResult.In the standard library,
Eitheris defined as a variant with two data constructors,LeftandRight:data Either a b = Left a | Right bYou can read this as “A value of type
Either a b, is either aLeftholding ana-value, or aRightholding ab-value”. Here bothLeft’s andRight’s have the same type, namelyEither a b. - Stephen_Herzig
Thanks, all! Very helpful, and solved the issue.