Extract Value From Script (Daml Syntax)
- VictorShneerOP
Hi, Daml community!
I’m trying to write a helper function with a notation:
extractFirstCidFromQueryResults: (Template t) => Script([(ContractId t, t)]) -> ContractId tUsually, in the script context, I do it like this:
firstCidFromQuery <- fst . head <$> query @Template partyNow I want to write a helper function to hide this part:
fst . head <$>And I can’t figure out how to do this… How can I do <$> as a first action in the function… I tried >>=, but it looks like I’m missing a concept here. Help!
Thanks!
PS
And I tried to query inside a function:
queryAndTakeFirstCid: (Template temp) => Party -> temp -> Script(ContractId temp) queryAndTakeFirstCid party temp = query @temp party >>= \case -- ^Not in scope: type variable ‘temp’ x:xs -> return $ fst x _ -> error "no cids" queryAndTakeFirstCid party MyTemplateI will highly appreciate it if you can give me a clue on how to construct a function like this.
- Matteo_Limberto 1
Hi @VictorShneer,
In the expression you would write within a script
firstCidFromQuery <- fst . head <$> query @Template partyyou are using the
<-operator to extract a value of typexfrom aScript x.Hence, the expression to the right of
<-must be wrapped in aScript. If you want to abstract this logic behind a helper function, the helper function should then return aScript (ContractId t)rather than just aContractId t.extractFirstCidFromQueryResults: (Template t) => Script([(ContractId t, t)]) -> Script (ContractId t)Additionally, I think the
Template ttype-class constraint is unnecessary.On your second point, you don’t really need the
@temptype specification (it is automatically inferred by the compiler) and can writequeryAndTakeFirstCid: (Template t) => Party -> Script (ContractId t) queryAndTakeFirstCid party = query party >>= \case x :: xs -> pure $ fst x [] -> error "no cids"Please, note that I used
::to pattern-match a non-empty list rather than:.Matteo