How to do type conversion for parameters during daml upgrading
- skylornaOP
- Stephen 1
As mentioned here, please don’t post screenshots of code/errors/logs; use ``` above and below as explained at the link to enclose a block of code.
skylorna:How should I convert?
It depends on what the types are.
If it’s a contract ID, then you probably need to upgrade that contract, too; if the contract ID is referenced from multiple Bet contracts, you probably need to upgrade it separately and pass the new ID in as a choice argument so that you do not turn one old pool contract into a bunch of new pool contracts.
Otherwise, well, suppose I said “write a function that converts from
[Int]to[Text]”. The only thing that is clear is that you have to do something, you can’t just return the argument; beyond that it depends on what your application needs. - bernhard 1
In the specific case here where you have a custom
BetStatustype, you need to write a conversion function.
What that looks like depends on the definition ofBetStatus. In all cases, I’ll assume that you have not changed the definition.If it’s an Enum:
data BetStatus = Status1 | Status2 | Status3You can write
betStatus = toEnum (fromEnum oldBet.betStatus)toEnum and fromEnum convert between enum types and Ints so this is just saying take the enum value in the same place in the enum.
If it’s a Record:
data BetStatus = BetStatus with field1 : T1 field2 : T2 field3 : T3You can define a function
upgradeBetStatus : Old.BetStatus -> New.BetStatus upgradeBetStatus Old.BetStatus{..} = New.BetStatus with ..and then
betStatus = upgradeBetStatus oldBet.betStatusIf it’s a variant:
data BetStatus = StatusType1 Payload1 StatusType2 Payload1 Payload2 StatusType3You can define a function
upgradeBetStatus : Old.BetStatus -> New.BetStatus upgradeBetStatus x = case x of Old.StatusType1 y -> New.StatusType1 y Old.StatusType2 y z -> New.StatusType2 y z Old.StatusType3 -> New.StatusType3and then
betStatus = upgradeBetStatus oldBet.betStatus - skylorna
bernhard:betStatusthank you very much

