跳轉到內容

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