为什么局部变量变量不受尊重?

发布于 2024-09-12 06:13:04 字数 1103 浏览 3 评论 0原文

在此代码片段中,最后的 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 中有类似 varval 关键字的东西吗?

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 技术交流群。

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

发布评论

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

评论(2

通知家属抬走 2024-09-19 06:13:04

变量在 REBOL 中是一个丑陋的词,一切——甚至单词——都是值。这不是什么语义新话,它有助于理解 REBOL 的流动方式。

我认为值包含在内存中的一个巨大数组中,其中 REBOL(语言)使用单词及其上下文来引用值并与值交互。大多数 REBOL 函数的运行无需复制这些值:

head lowercase next uppercase str: "abcd"
remove back tail str

这是 REBOL 最高效的功能之一 - 您不需要中间进程的副本,因为需要这样的副本是浪费。想象一下,每次使用replaceuppercaseto-camel-case 时,数组都会不断增长,值就会重复。整个过程可以在修改而不是重复的前提下构建 - 事实上,可以构建上下文而不必返回值:

remove-spaces: use [space mark][
    space: charset " ^-"
    [any [mark: space (remove mark) | skip]]
]

parse/all str: "Should Be No Spaces" remove-spaces

然后技巧就变成知道在哪里复制值,我认为恰好与 REBOL 简洁表达的天赋相交:

parse/all link: copy title: "An Article on REBOL" remove-spaces
print ["(" link ")" title]

to-camel-case copy field-label

当然,修改也有其局限性。有时,从头开始构建更干净的建筑。

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:

head lowercase next uppercase str: "abcd"
remove back tail str

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 or to-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:

remove-spaces: use [space mark][
    space: charset " ^-"
    [any [mark: space (remove mark) | skip]]
]

parse/all str: "Should Be No Spaces" remove-spaces

The trick then becomes knowing where to copy values, and I think happens to intersect with REBOL's gift for concise expression:

parse/all link: copy title: "An Article on REBOL" remove-spaces
print ["(" link ")" title]

to-camel-case copy field-label

And of course, modification has its limitations. Sometimes it is cleaner building from scratch.

童话里做英雄 2024-09-19 06:13:04

您的驼峰式大小写函数对原始值进行操作,因此如果您想保留原始值,则需要复制它,并返回更改后的值。由于您的函数作用于模板,因此需要复制模板,对吧?

所以,这样的事情应该有效:

fill-template-body: func[ labels [block!] /local field-labels-types vars fields-names-types][
  field-labels-types: copy labels
..

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:

fill-template-body: func[ labels [block!] /local field-labels-types vars fields-names-types][
  field-labels-types: copy labels
..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文