跳转到内容

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]
]
华夏公益教科书