为什么局部变量变量不受尊重?
在此代码片段中,最后的 fields-types 是由 to-camel-case 函数修改的,而不是作为局部变量传递给父函数
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
to-camel-case: function [name] [
name/1: lowercase name/1
replace/all name space ""
]
fill-template-body: func [
field-labels-types [block!] /local vars fields-names-types
] [
vars: [member-name member-type]
field-names-types: copy []
foreach [field-label field-type] field-labels-types [
append field-names-types to-camel-case field-label
append field-names-types field-type
]
]
fill-template-body fields-types
:给出:
>> fill-template-body fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>> fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>>
虽然我希望字段类型保持不变:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
当然,我可以尝试通过修改 to-camel-case
以使用名称副本来规避此问题,但这不是我认为的我应该做的。
Scala 中有类似 var
和 val
关键字的东西吗?
In this code snippet, fields-types
in the end is modified by the to-camel-case
function, vs. being passed as a local variable to the parent function:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
to-camel-case: function [name] [
name/1: lowercase name/1
replace/all name space ""
]
fill-template-body: func [
field-labels-types [block!] /local vars fields-names-types
] [
vars: [member-name member-type]
field-names-types: copy []
foreach [field-label field-type] field-labels-types [
append field-names-types to-camel-case field-label
append field-names-types field-type
]
]
fill-template-body fields-types
Execution gives:
>> fill-template-body fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>> fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>>
Whereas I would want that fields-types to stay invariant:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
Of course I can try to circumvent this by modifying to-camel-case
to use a copy of name, but that is not something I think I should have to do.
Is there something like the var
and val
keywords in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量在 REBOL 中是一个丑陋的词,一切——甚至单词——都是值。这不是什么语义新话,它有助于理解 REBOL 的流动方式。
我认为值包含在内存中的一个巨大数组中,其中 REBOL(语言)使用单词及其上下文来引用值并与值交互。大多数 REBOL 函数的运行无需复制这些值:
这是 REBOL 最高效的功能之一 - 您不需要中间进程的副本,因为需要这样的副本是浪费。想象一下,每次使用
replace
、uppercase
或to-camel-case
时,数组都会不断增长,值就会重复。整个过程可以在修改而不是重复的前提下构建 - 事实上,可以构建上下文而不必返回值:然后技巧就变成知道在哪里复制值,我认为恰好与 REBOL 简洁表达的天赋相交:
当然,修改也有其局限性。有时,从头开始构建更干净的建筑。
Variable is an ugly word in REBOL, everything - even words - are values. This isn't some semantic newspeak, it's helpful in understanding the way in which REBOL flows.
I think of values as being contained within one giant array in memory, where REBOL (the language) uses words and their contexts to reference and interact with the values. Most REBOL functions operate without duplicating these values:
This is one of REBOL's most efficient features - you don't need copies for intermediary processes, to require such is wasteful. Think of that array growing where every time you use
replace
,uppercase
orto-camel-case
a value is duplicated. Whole processes can be built on the premise of modification rather than duplication - indeed a context can be built without necessarily having to return a value:The trick then becomes knowing where to copy values, and I think happens to intersect with REBOL's gift for concise expression:
And of course, modification has its limitations. Sometimes it is cleaner building from scratch.
您的驼峰式大小写函数对原始值进行操作,因此如果您想保留原始值,则需要复制它,并返回更改后的值。由于您的函数作用于模板,因此需要复制模板,对吧?
所以,这样的事情应该有效:
Your camel case function operates on the original value so if you want to preserve the original value, you need to copy it, and return the altered value. Since your function acts on a template it needs to copy the template right??
So, something like this should work: