VALUE如何?功能工作?

发布于 2024-09-07 15:31:38 字数 1059 浏览 3 评论 0原文

我已将一些代码简化为一个小示例,该代码测试名为 class-name 的变量是否分配有值:

ask-params: function [
    config-file [file!]
    default-class-name
    default-fields
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        ;-- omit code in this branch for now
    ]
]

ret-block: ask-params %simple-class.params.txt "Person" "First Name, Last Name"

表达式 value? 'class-name 在这里返回 false。另一方面,如果我用赋值填充缺失的分支:

ask-params: function [
    config-file [file!]
    default-class-name
    default-fields
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        class-name: default-class-name
    ]
]

ret-block: ask-params %simple-class.params.txt "Person" "First Name, Last Name"

这将返回 true for value? '类名.但在第二种情况下,class-name: default-class-name 甚至还没有执行。

我认为类名不应该存在于内存中,所以值? 'class-name 应该返回 false。为什么 value? 返回 true ?

I have reduced down to a small example some code that I have, which tests for whether a variable called class-name has a value assigned to it:

ask-params: function [
    config-file [file!]
    default-class-name
    default-fields
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        ;-- omit code in this branch for now
    ]
]

ret-block: ask-params %simple-class.params.txt "Person" "First Name, Last Name"

The expression value? 'class-name returns false here. On the other hand, if I fill in the missing branch with an assignment:

ask-params: function [
    config-file [file!]
    default-class-name
    default-fields
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        class-name: default-class-name
    ]
]

ret-block: ask-params %simple-class.params.txt "Person" "First Name, Last Name"

This will return true for value? 'class-name. But in this second case, class-name: default-class-name isn't even executed yet.

I would think that class-name shouldn't exist in memory, so value? 'class-name should be returning false. Why is value? returning true instead?

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

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

发布评论

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

评论(4

何以心动 2024-09-14 15:31:38

您正在使用函数。这会扫描函数体并为您预先创建局部变量,并初始化为 NONE。这就是为什么值? 'class-name 变为 true(因为 NONE 是变量的合法值,与“未设置”的情况不同)。

如果您使用 func 来代替,那么两者都会返回 false。

You are using function. This scans the body of the function and pre-creates the local variables for you, initialized to NONE. That's why value? 'class-name becomes true (because NONE is a legal value for a variable, distinct from the situation of being "unset").

If you used func instead, then both would return false.

策马西风 2024-09-14 15:31:38

我不认为 function 的行为与 func /local 不同。看这些例子:

>> f: func [/x] [value? 'x]
>> f
== true

我没有给 x 任何值,但它说它有一个值。 /local 也是如此,

>> f: func [/local x] [value? 'x]
>> f
== true

因为当您将变量设置为本地(或细化)时,这意味着您已经为其设置了一个值(没有),这就是 function 所做的。

I don't think function behaves differently than func /local. Look at these examples:

>> f: func [/x] [value? 'x]
>> f
== true

I didn't give any value to x, but it says it HAS a value. Same for /local

>> f: func [/local x] [value? 'x]
>> f
== true

Because when you make a variable local (or a refinement) then it means you already set a value for it (which is none) and that is what function does.

蒗幽 2024-09-14 15:31:38

这里我向您展示了两个不使用 FUNCTION 的示例,但在其他方面与您的代码等效:

ask-params: func [config-file [file!] default-class-name default-fields] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ][
    ]
]

ask-params: func [
    config-file [file!] default-class-name default-fields /local class-name
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ][
    ]
]

第一个示例中的 value? 函数生成 #[false],而在第二个示例中它产生#[true]。这是因为“未使用的细化”(实际调用中未使用的细化)后面的“细化参数”与细化变量一起被初始化为 #[none!] 。这也适用于 /local 变量,因为 /local 细化与其他函数细化没有什么不同(除了事实上,它是使用它的约定)定义局部变量)。

由于 function 生成器使用 /local 方法“在幕后”实现局部变量,因此上述描述也适用于它生成的所有函数。

Here I show you two examples not using FUNCTION, but otherwise equivalent to your code:

ask-params: func [config-file [file!] default-class-name default-fields] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ][
    ]
]

ask-params: func [
    config-file [file!] default-class-name default-fields /local class-name
] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ][
    ]
]

While the value? function in the first example yields #[false], in the second example it yields #[true]. That is because the "refinement arguments" following an "unused refinement" (a refinement that is not used in the actual call) are initialized to #[none!] together with the refinement variable. This applies to the /local variables as well, since the /local refinement does not differ from other function refinements (except for the fact, that it is a convention to use it to define local variables).

Since the function generator uses the /local method to implement local variables "under the hood", the above description applies to all functions it generates as well.

嘿咻 2024-09-14 15:31:38

还有另一种方法,避免使用FUNC/LOCAL,但仍然允许使用FUNCTION。

那就是不使用SET-WORD!为作业。相反,请在 LIT-WORD 上使用 SET 函数!

ask-params: function [config-file [file!] default-class-name default-fields] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        set 'class-name default-class-name
    ]
]

您将获得 value? 函数的 #[false]。但是,对 SET 的调用将在全局环境中设置class-name...而不是本地环境。

There is another way, which avoids using FUNC/LOCAL and still allows the use of FUNCTION.

That is to not use a SET-WORD! for the assignment. Instead use the SET function on a LIT-WORD!

ask-params: function [config-file [file!] default-class-name default-fields] [
    probe value? 'class-name
    input
    either (value? 'class-name) [
        probe class-name
    ] [
        set 'class-name default-class-name
    ]
]

You will get #[false] for the value? function. However, the call to SET will be setting class-name in the global environment...not as a local.

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