跳轉到內容

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
            ]
        ]
    ]]
華夏公益教科書