内置 FUNCTION 和 FUNCS 附加组件有什么区别?

发布于 2024-09-10 17:34:03 字数 862 浏览 1 评论 0原文

Rebol 3 中有一个FUNCTION 的新实现,它允许将变量自动绑定到默认情况下是本地上下文。

FUNCTION好像和VALUE有问题?测试,因为即使变量尚未在运行时设置,它也会返回 TRUE:

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]
    ]
]

如果您调用 FOO,您将得到:

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

这不是 FUNC 的工作方式(它只表示 BAR 在赋值后有一个值)。但是 FUNC 不会自动使变量成为本地变量。

我在 Ladislav Mecir 创建的库中找到了 FUNCS 原语。它有何不同,是否有相同的缺点?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-09-17 17:34:03

主要区别在于,FUNCTION 深度搜索正文中的设置词,而 FUNCS 只是浅层搜索。 FUNCS 还使用略有不同的规范。

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

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

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

The main difference is, that FUNCTION deep-searches for set-words in the body, while FUNCS just shallow-searches for them. FUNCS also uses a slightly different specification.

FUNCS has been around for quite some time (a name change occurred not long ago, though).

That VALUE? function "problem" is related to the fact that the local variables of functions (even if you use FUNC with /LOCAL to explicitly declare them) are initialized to NONE. That causes the VALUE? function to yield TRUE even when the variables are "not initialized yet".

Generally, I do not see this "initialized with NONE" a "big deal", although this behavior is not the same as the behavior of either global or object variables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文