REBOL 3.0

RFC: func-local, funclo, funco, funo, funx?

Carl Sassenrath, CTO
REBOL Technologies
19-Jun-2008 22:52 GMT

Article #0141
Main page || Index || Prior Article [0140] || Next Article [0142] || 44 Comments || Send feedback

Request for comments:

There is a common function-defining function that will be added to R3: It is an effort-saving method that uses the set-words within the function body (and its sub-blocks) to determine the locals of the function.

So, in this example:

f: func-local [arg] [
    len: length? arg
    if len > 1 [tail: tail? arg]
    ...       
]

The len and tail words are automatically made local to the function. They do not need to be included in the arg block.

This function is handy for many cases that do not set variables that are part of other contexts (such as globals), and it eliminates the need for the coder to worry about leaky locals.

To set an external variable, you would need to use the set function or a path set. For example:

set 'ext-var 10
ctx/var: 10

The func-local function is currently defined as:

func-local: func [
    {Defines a user function assuming set-words are locals.}
    spec [block!] {Function arguments (and help strings)}
    body [block!] {The body block of the function}
    /local ctx
][
    spec: copy/deep spec
    body: copy/deep body
    ctx: make object! 4
    bind/set body ctx ; see note below
    unless find spec /local [append spec /local]
    append spec words-of ctx
    make function! reduce [spec body]
]

Note: In the future we could improve this code a bit by using the internal code that bind/set uses to quickly gather set-words. This would require that we export this function (collect-words).

Issue: What to call it?

The problem is what to call this function.

We can call it func-local, or we can shorten it to funclo, or even funco, funo, or funx. These may seem like silly names, but note that the word func is just a shortcut name for function. So, shortcut names are fine... you just have to learn them.

I'm open to suggestions as well.

44 Comments

REBOL 3.0
Updated 29-Mar-2024 - Edit - Copyright REBOL Technologies - REBOL.net