跳转到内容

Rebol 编程/funct

来自维基教科书,开放的书籍,开放的世界
FUNCT spec body /with object /extern words 

定义一个函数,所有 set-words 作为局部变量。

FUNCT 是一个函数值。

  • spec -- 帮助字符串(可选)后面跟着参数词(以及可选类型和字符串)(类型:块)
  • body -- 函数的主体块(类型:块)
  • /with -- 定义或使用持久对象(自身)
    • object -- 对象或规范(类型:对象块)
  • /extern
    • words -- 这些词不是局部的(类型:块)

(特殊属性)

[编辑 | 编辑源代码]
  • catch

源代码

[编辑 | 编辑源代码]
funct: func [
    "Defines a function with all set-words as locals." 
    [catch] 
    spec [block!] {Help string (opt) followed by arg words (and opt type and string)} 
    body [block!] "The body block of the function" 
    /with "Define or use a persistent object (self)" 
    object [object! block!] "The object or spec" 
    /extern words [block!] "These words are not local" 
    /local r ws wb a
][
    spec: copy/deep spec 
    body: copy/deep body 
    ws: make block! length? spec 
    parse spec [any [
            set-word! | set a any-word! (insert tail ws to-word a) | skip
        ]] 
    if with [
        unless object? object [object: make object! object] 
        bind body object 
        insert tail ws first object
    ] 
    insert tail ws words 
    wb: make block! 12 
    parse body r: [any [
            set a set-word! (insert tail wb to-word a) | 
            hash! | into r | skip
        ]] 
    unless empty? wb: exclude wb ws [
        remove find wb 'local 
        unless find spec /local [insert tail spec /local] 
        insert tail spec wb
    ] 
    throw-on-error [make function! spec body]
]
华夏公益教科书