如何在 DAML 中将 Party 强制转换为文本,反之亦然?

How to typecast Party to a Text or vice versa in DAML?

我想检查 DAML 代码中的条件,但一个值是 party,另一个是 text,因此出现错误:

• Couldn't match type ‘Party’ with ‘Text’ arising from a functional dependency between: constraint ‘DA.Internal.Record.HasField "owner" AccountInfo Text’ arising from a use of ‘DA.Internal.Record.getField’ instance ‘DA.Internal.Record.HasField "owner" AccountInfo Party’ at

if( login.party == "friend" || logout.party == "friend)
   userCId <- create Users with userType= "Friendly User",..
                            return (Right  userCId)

在您的 Party 值上调用 show 应该可以解决问题。

if show login.party == "friend" ...

如 Shayne 所述,您可以使用 show(或 partyToText)转换为 Text。但是,我认为这不是解决这个问题的正确方法。各方应被视为抽象标识符,虽然 DAML 沙箱允许您使用任意字符串,但对于其他分类帐而言并非如此。

我建议不要针对特定​​方文字对检查进行硬编码,而是使用附加字段 friend : Party 扩展模板,然后与之进行比较。然后,当您创建模板时,您可以在沙盒上将 friend 设置为 "friend",但您也可以将其设置为其他内容。

所以最后你最终替换了

template C with
  …
  choice C : ()
    controller …
    do if login.party == "friend"
       …

与以下

template C with
  …
  friend : Party
  choice C : ()
    controller …
    do if login.party == friend
       …