Abstract cross-cutting concerns away from template choices
- rexngOP 1
Let’s say I have many templates that have a
changeTimefield liketemplate A with creator : Party changeTime : Time where signatory creator choice Foo : ContractId A controller creator do -- Update some other fields as well, skipped for brevity currentTime <- getTIme create this with changeTime = currentTime template B with creator : Party changeTime : Time where signatory creator choice Bar : ContractId B controller creator do -- Update some other fields as well, skipped for brevity currentTime <- getTIme create this with changeTime = currentTimeI’d like to know if there is a way to abstract the
currentTime <- getTime; create this with changeTime = currentTimeaway from the choice implementations, but making sure all templates having achangeTimefield will automatically ‘inherit’ (for lack of a better term) the behaviour of getting itschangeTimefield updated whenFooorBaris exercised.I am thinking interfaces might help but I am not exactly sure.
- cocreature 1
I would just factor this out into a function. You don’t get the automatic inheritance that you’re asking for but I think being explicit here is preferable. You could in theory try to build some interface based solution but interfaces are a fairly heavy weight solution to achieve decoupling. They’re not that great for just reducing boilerplate so I wouldn’t use them here:
template A with creator : Party changeTime : Time where signatory creator choice Foo : ContractId A controller creator do updateTime this template B with creator : Party changeTime : Time where signatory creator choice Bar : ContractId B controller creator do updateTime this updateTime : (HasField "changeTime" t Time, HasCreate t) => t -> Update (ContractId t) updateTime t = do currentTime <- getTime create t with changeTime = currentTime - rexng
Thank you @cocreature!