如何在“确保”子句中评估两个条件中的任何一个?

How to have either of two conditions evaluated in the `ensure` clause?

如何以 or 条件的形式将值传递给 ensure 子句?

template ABC

ensure (abcd)? (xyz) || (abc)

是否可以这样做(可能使用其他语法)来传递 ensure 两个谓词,其中任何一个都必须被评估?

如果您只想表达一个布尔表达式,其中两个值中的任何一个为真,|| 就可以。

不过,您使用的语法表明您可能有兴趣表达如下内容(伪代码):

if abcd is true
  fail to create the contract if xyz is true
else
  fail to create the contract if abc is true

ensure 子句将 "predicate that must be true, otherwise, contract creation will fail" 作为参数(文档 here)。

谓词只是一个函数,returns 一个布尔值,真或假。

与其他语言相反(您建议的语法似乎表明您熟悉 C、C++ 或 Java),在 DAML 中 if 是一个表达式,这意味着它 returns一个值。这意味着您可以像在 Java.

中使用 <condition> ? <if_true> : <if_false> 构造一样使用它

希望以下示例能帮助您:

daml 1.2
module Main where

template Main
  with
    owner : Party
    cond1 : Bool
    cond2 : Bool
    cond3 : Bool
  where
    signatory owner
    ensure if cond1 then cond2 else cond3 -- HERE!

test = scenario do
  p <- getParty "party"
  submit p do create $ Main p True True False
  submit p do create $ Main p False False True
  submitMustFail p do create $ Main p False True False

请注意,根据具体情况,您可能希望使用布尔运算符将同一子句表达为单个条件:

ensure cond1 && cond2 || cond3