DAML 中的 、error
和有什么区别?fail
abort
assert
问问题
265 次
1 回答
1
fail
和abort
是相同功能的别名,使用abort
更普遍。它们用于使Action
likeUpdate
和失败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 回答