为什么 Scala 语言要求初始化实例变量而不是依赖默认值?
Scala 语言要求您在使用实例变量之前对其进行初始化。但是,Scala 不为变量提供默认值。相反,您必须使用通配符下划线手动设置其值,它的作用类似于默认值,如下所示
var name:String = _
我知道,我知道...我可以在类定义中定义一个构造函数,它将我们的实例变量作为参数,所以Scala不会强制其初始化,如下所示
class Person(var name:String)
但是,我需要在主体中声明它,因为我需要使用ElementType为FIELD或METHOD的Java注释;也就是说,它只能应用于类主体中声明的实例变量或方法。
问题:为什么 Scala 语言要求您初始化一个实例变量(无论是默认值还是任何您想要的值)在类主体中声明,而不是依赖默认值?
Scala language requires you initialize your instance variable before using it. However, Scala does not provide a default value for your variable. Instead, you have to set up its value manually by using the wildcard underscore, which acts like a default value, as follows
var name:String = _
I know, i know... I can define a constructor in the class definition, which takes as parameter our instance variable, so Scala does not force its initialization as shown below
class Person(var name:String)
However, i need to declare it in the body because i need to use a Java annotation whose ElementType is FIELD or METHOD; that is, it can just be applied to either a instance variable or method declared in the body of our class.
Question: Why does Scala language require you initialize a instance variable - be it a default value _ or whatever you want - declared in the body of a class instead of relying on a default value ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用如下所示的代码,则您声明的名称应该是抽象的:
我想您已经知道了。所以你的问题是句法性质的。
答案是与其他可能的抽象候选人保持一致。
假设您想做这样的事情:
最后三个示例甚至无法编译。现在假设您想做这样的事情:
从我的角度来看,即使是几乎不懂 Scala 的人也只能看到声明。没有办法误解它们,因为除了声明之外没有其他东西。当您来自另一种语言并假设有一些默认值时,就会出现唯一的混乱。但是,当您看到第一个示例(具有默认值的示例)时,这种混乱就消失了。顺便说一句,您的类必须是抽象层次结构的一部分才能允许声明抽象成员,因此即使您是该语言的新手,您也已经从编译器获得了一些额外的帮助。
我希望这能回答您的问题并祝您编码愉快。
If you use code like the following you are declaring, that the
name
should be abstract:I suppose you already knew that. So your question is rather of the syntactical nature.
The answer is consistency with other possible abstract candidates.
Suppose you want to do something like this:
The last three examples don't even compile. Now suppose you want to do something like this:
From my point of view, even someone who barely understands Scala sees only declarations. There is no way to misinterpret them, because there is nothing there else than declarations. The one and only confusion arises when you come from another language and assume for a second that there are some default values. But this confusion is gone as soon as you see the first example (the one with the default values). And btw your class has to be a part of an abstract hierarchy in order to be allowed to declare abstract members, so even if you are new to the language you already get some extra help from the compiler.
I hope this answers your question and happy coding.
当您将注释指定为构造函数参数时,您可以应用该注释。此外,您可能需要使用元注释来限制您正在使用的注释应用于哪个目标 - 请参阅 http://www.scala-lang.org/api/2.10.2-RC2/index.html#scala.annotation.meta.package
你的不过,关于“依赖默认值”的问题有些不清楚。使用下划线进行初始化相当于将变量的值赋给 null。您还想到什么其他默认设置?
You can apply the annotation when you specify it as a constructor argument. Also, you may need to use a meta-annotation to restrict which target the annotation you're using is applied to - see http://www.scala-lang.org/api/2.10.2-RC2/index.html#scala.annotation.meta.package
Your question about "relying on a default value" is somewhat unclear, though. Initialization using an underscore corresponds to assigning the value of the variable to null. What other default are you thinking of?
Scala 对于类主体中的“var name: String”没有问题。你尝试过吗?但这并不意味着你想要它的意思。这是一个抽象变量。
Scala has no issue with "var name: String" in the class body. Did you try it? It doesn't mean what you want it to mean, though. It's an abstract var.