跳转到内容

Rebol 编程/assert

来自维基教科书,开放世界中的开放书籍
ASSERT conditions /type 

断言条件为真,否则抛出断言错误。

ASSERT 是一个函数值。

  • 条件 -- (类型:块)
  • /type -- 安全检查变量(单词)的数据类型

(特殊属性)

[编辑 | 编辑源代码]
  • 捕捉
  • 抛出

源代码

[编辑 | 编辑源代码]
assert: func [
    {Assert that condition is true, else throw an assertion error.} 
    [catch throw] 
    conditions [block!] 
    /type "Safely check datatypes of variables (words)" 
    /local w t
][throw-on-error [
        either type [
            parse conditions [any [
                    [set w word! | set w skip (
                            cause-error 'script 'invalid-arg type? get/any 'w
                        )] 
                    [set t [block! | word!] (
                            unless find to-typeset t type? get/any w [
                                make error! join "datatype assertion failed for: " w
                            ]
                        ) | set t skip (
                            cause-error 'script 'invalid-arg type? get/any 't
                        )]
                ]]
        ] [
            any [
                all conditions 
                make error! join "assertion failed for: " mold conditions
            ]
        ]
    ]]
华夏公益教科书