为什么Scala 支持影子变量?
我认为影子变量太危险而不能使用它们。为什么 Scala 支持这种语言结构?应该有一些强有力的理由,但我找不到它。
I think that shadow variables are too dangerous to use them. Why does Scala support this language construct? There should be some strong reason for that, but I cant find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提醒一下:当一个变量、方法或类型在内部作用域中声明时,它会遮蔽另一个同名的变量、方法或类型,从而无法引用该变量、方法或类型。以不合格的方式(或者有时根本不)。 Scala 与 Java 一样,允许阴影。
我看到的一个可能的原因是,在 Scala 中,经常有许多嵌套作用域,每个嵌套作用域都相对较短(与 Java 或 C++ 相比)。事实上,块可以在需要表达式的任何地方开始,从而开始一个新的作用域。因此,平均而言,在内部作用域中使用隐藏名称更接近其声明并且不那么模糊。
此外,内联闭包通常会导致程序员在已经拥挤的作用域中需要新的变量名称。允许遮蔽还允许继续使用足够切题的描述性名称,即使它们与所有已经使用的名称相同,而不是发明其他奇怪的名称 - 比如在它们前面加上
my
、local
,或者(更糟糕的)_
或单字母名称……对于好的 IDE,阴影不再是一个问题,例如可以在源代码中突出显示 的声明和引用,光标下的变量。
这里只是我的两分钱……
Just as a reminder: A variable, method, or type is said to shadow another variable, method, or type of the same name when it is declared in an inner scope, making it impossible to refer to the outer-scope entity in an unqualified way (or, sometimes, at all). Scala, just like Java, allows shadowing.
One possible reason I could see is that in Scala, it is frequent to have many nested scopes, each of which is relatively short (compared to e.g. Java or C++). Indeed, a block can start anywhere where an expression is expected, thus starting a new scope. The use of the shadowing names in the inner scopes is thus, on average, closer to their declaration and less ambiguous.
Moreover, inline closures often lead the programmer to need new variable names in a scope that is already crowded. Allowing shadowing also allows to keep using descriptive names that are sufficiently to the point, even if they are the same as al already used name, instead of inventing other weird names — like prefixing them with
my
,local
, or (worse)_
or single-letter names…Shadowing becomes less of a problem with good IDEs which can e.g. highlight in your source code the declaration of, and the references to, the variable under the cursor.
Just my two cents here…
你有权随心所欲地思考。然而,由于你没有提供任何数据、研究甚至理由,所以这种观点没有任何价值。
因为它很有用。程序员不需要仅仅因为作用域中的某些标识符已经在使用它而发明任意标识符名称。
它也使通配符导入更加有用,因为它消除了仅仅因为第三方添加了您正在使用的标识符而导致编译中断的可能性。
为什么要有一个强有力的理由呢?它有优点,并且在没有缺点的情况下(你没有提出任何缺点),这就足够了。
编辑
在回答所解释的缺点时,我必须说这是阴影的一种特殊情况。遮蔽还会影响导入中的所有内容(通过
import
语句或通过嵌套的package
语句)以及同一包中的所有内容。让我们看一些例子:
这将是一种非常烦人的语言。
You are entitled to think whatever you want. However, since you have provided no data, studies or even reasons, that opinion has no value.
Because it is useful. Programmers don't need to invent arbitrary identifier names just because some identifier in scope is already using it.
It makes wildcard imports more useful as well, as it removes the chance of a compile breaking just because a third party added a identifier you are using.
Why should there be a strong reason for that? There are advantages to it, and in the absence of disadvantages (you presented none), that is enough.
EDIT
In answer to the disadvantages explained, I must say that is a special case of shadowing. Shadowing also affects everything in import, either through
import
statements or through nestedpackage
statements, and everything that is in the same package.Let's see some examples:
That would make for a very annoying language.