Hello all,

I've been working on an enhancement request regarding argument parsing.
The idea is based on the TagList concept of AmigaOS.
I've adapted it somewhat to fit better into the REBOL philosophy.

The idea is to allow functions to have many arguments, of which most
are optional and have default values.

an example function declaration:

my-function: func [
    "My function"
    a [string!] "required argument"
    /b "A refinement"
    c: "Hello" [string!] {An optional argument with the default value "Hello"}
] [
    print mold [a c]
]

example of usage:

>> my-function "Test1"
Test1 Hello
>> my-function/c "Test2" "Boo"
Test2 Boo
>> my-function/tags "Test3" [c: "Test"]
Test3 Test


I've made a function that accomplish most of this. It's attached.
It's called 'tagfunc, and works by modifying the code and argument
blocks, before passing them to 'func.

Before making it an "official" request, I'd like to invite everyone
interested to discuss this concept - ie does it appeal to anyone but
me :-)

Any comments greatly appriciated!

Best regards
Thomas Jensen
REBOL [
    Title:  "Taglist alike function definitions"
]



tagfunc: func [args code /allow-tagblock /local word def-value val-word] [
    args: head copy args
    code: head copy code

    forall args [
        if set-word? first args [
            word: first args
            val-word: to word! rejoin [:word "-value"]
            def-value: second args
            remove/part args 2
            args: insert args (to refinement! :word)
            insert args val-word
            code: insert code compose/deep [
                (:word) either (to word! :word) [(val-word)] [(either block? :def-value [reduce [def-value]] [def-value])]
            ]
        ]
    ]

    if allow-tagblock [
        args: insert args [/tags tag-list [block!] "A block of words to set, ie [size: 50]"]
        code: insert code [
            if tags [
                do bind (compose/deep tag-list) 'tags
            ]
        ]
    ]

    func (head args) (head code)
]


; ----------------------------
; -- tests below this point --
; ----------------------------


my-function: tagfunc/allow-tagblock [
    "My function"
    a [string!] "required argument"
    /b "A refinement"
    c: "Hello" [string!] {An optional argument with the default value "Hello"}
] [
    print [a c]
]


my-function "Test1"
my-function/c "Test2" "Boo"
my-function/tags "Test3" [c: "Test"]

help my-function
source my-function

Reply via email to