跳轉到內容

Rebol 程式設計/閉包

來自華夏公益教科書
CLOSURE spec body 

定義閉包函式。

CLOSURE 是一個函式值。

  • spec -- 幫助字串(可選)後跟引數詞(以及可選型別和字串)(型別:塊)
  • body -- 函式的主體塊(型別:塊)

(特殊屬性)

[編輯 | 編輯原始碼]
  • 捕捉

原始碼

[編輯 | 編輯原始碼]
closure: func [
    "Defines a closure function." 
    [catch] 
    spec [block!] {Help string (opt) followed by arg words (and opt type and string)} 
    body [block!] "The body block of the function" 
    /local spc bdy word
][
    spc: make block! 1 + (2 * length? spec) 
    insert/only spc [throw] 
    bdy: make block! 5 + length? spec 
    insert bdy reduce [:do :make :function! spc body] 
    parse spec [any [
            set-word! | set word any-word! (
                insert tail bdy to word! :word 
                insert tail spc to get-word! :word 
                insert/only tail spc [any-type!]
            ) | skip
        ]] 
    throw-on-error [make function! spec bdy]
]
華夏公益教科書