2

DAML 中的 、error和有什么区别?failabortassert

4

1 回答 1

1

failabort是相同功能的别名,使用abort更普遍。它们用于使ActionlikeUpdate和失败Scenario,但仍返回适当类型的值。以下场景非常好,因为s从未实际执行过:

t = scenario do
  let
    s : Scenario () = abort "Foo"
  return ()

当你想要一个Scenario或的分支Update导致失败时,使用abort. 例如,以下Scenario将根据 的值成功或失败abortScenario

t2 = scenario do
  let abortScenario = True
  if abortScenario
    then abort "Scenario was aborted"
    else return ()

assert只是一个包装器abort

-- | Check whether a condition is true. If it's not, abort the transaction.
assert : CanAbort m => Bool -> m ()
assert = assertMsg "Assertion failed"

-- | Check whether a condition is true. If it's not, abort the transaction
-- with a message.
assertMsg : CanAbort m => Text -> Bool -> m ()
assertMsg msg b = if b then return () else abort msg

使用 几乎总是更好abortMsg,因为您可以提供信息丰富的错误消息。

error用于定义偏函数。它不返回值,但会导致解释器立即退出并显示给定的错误消息。例如

divide : Int -> Int -> Int
divide x y
  | y == 0 = error "Division by Zero!"
  | otherwise = x / y

DAML 被急切地执行,因此您必须非常小心error. 以下场景将失败,即使e未使用。

s = scenario do
  let
    e = divide 1 0
  return ()
于 2019-09-04T07:55:26.547 回答