VALUE如何?功能工作?
我已将一些代码简化为一个小示例,该代码测试名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在使用
函数
。这会扫描函数体并为您预先创建局部变量,并初始化为 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 whyvalue? '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.我不认为
function
的行为与func /local
不同。看这些例子:我没有给 x 任何值,但它说它有一个值。 /local 也是如此,
因为当您将变量设置为本地(或细化)时,这意味着您已经为其设置了一个值(没有),这就是
function
所做的。I don't think
function
behaves differently thanfunc /local
. Look at these examples:I didn't give any value to x, but it says it HAS a value. Same for /local
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.这里我向您展示了两个不使用 FUNCTION 的示例,但在其他方面与您的代码等效:
第一个示例中的
value?
函数生成#[false]
,而在第二个示例中它产生#[true]
。这是因为“未使用的细化”(实际调用中未使用的细化)后面的“细化参数”与细化变量一起被初始化为#[none!]
。这也适用于/local
变量,因为/local
细化与其他函数细化没有什么不同(除了事实上,它是使用它的约定)定义局部变量)。由于
function
生成器使用/local
方法“在幕后”实现局部变量,因此上述描述也适用于它生成的所有函数。Here I show you two examples not using FUNCTION, but otherwise equivalent to your code:
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.还有另一种方法,避免使用FUNC/LOCAL,但仍然允许使用FUNCTION。
那就是不使用SET-WORD!为作业。相反,请在 LIT-WORD 上使用 SET 函数!
您将获得
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!
You will get
#[false]
for thevalue?
function. However, the call to SET will be settingclass-name
in the global environment...not as a local.