2

There is a new implementation of FUNCTION in Rebol 3, which allows making variables automatically bound to local context by default.

FUNCTION seems to have a problem with the VALUE? test, as it returns TRUE even if a variable has not been set at runtime yet:

foo: function [] [
    if value? 'bar [
        print [{Before assignment, bar has a value, and it is} bar]
    ]

    bar: 10

    if value? 'bar [
        print [{After assignment, bar has a value, and it is} bar]
    ]
]

If you call FOO you will get:

Before assignment, bar has a value, and it is none
After assignment, bar has a value, and it is 10

That is not the way FUNC works (it only says BAR has a value after the assignment). But then FUNC does not make variables automatically local.

I found the FUNCS primitive here, in a library created by Ladislav Mecir. How is it different, and does it have the same drawbacks?

http://www.fm.vslib.cz/~ladislav/rebol/funcs.r

4

1 回答 1

3

主要区别在于,FUNCTION 对正文中的集合词进行深度搜索,而 FUNCS 只是对它们进行浅层搜索。FUNCS 也使用稍微不同的规范。

FUNCS 已经存在了很长一段时间(不过不久前发生了名称更改)。

那个价值?函数“问题”与函数的局部变量(即使您使用带有 /LOCAL 的 FUNC 显式声明它们)初始化为 NONE 的事实有关。这会导致 VALUE?即使变量“尚未初始化”,函数也会产生 TRUE。

一般来说,我不认为这种“用 NONE 初始化”是“大问题”,尽管这种行为与全局或对象变量的行为不同

于 2010-07-17T11:09:11.283 回答