Scala Constructor Parameters

折月煮酒 提交于 2019-12-17 19:57:02

问题


What is the difference between a private var constructor parameter and a constructor parameter without val/var? Are they same in terms of scope/visibility?

Ex:

class Person(private var firstName:String, lastName:String)

回答1:


Yes, there are two important differences. First for the easy one: constructor parameters without the var or val keywords are not mutable variables—their values can't be changed in the body of the class.

Even if we restrict ourselves to the val keyword, though, there's still a difference between private val and keyword-less parameters. Consider the following:

class Person(private val firstName: String, lastName: String)

If we look at the compiled class with javap -v Person, we'll see that it only has one field, for firstName. lastName is just a constructor parameter, which means it may be garbage-collected after the class is initialized, etc.

The compiler is smart enough to know when the value of lastName will be needed after initialization, and it will create a field for it in that case. Consider the following variation:

class Person(private val firstName: String, lastName: String) {
  def fullName = firstName + " " + lastName
}

The compiler can tell that it may need the value of lastName later, and if we check javap again we'll see that the class has two fields (note that if we'd defined fullName as a val instead of a def, it'd only have one field).

Lastly, note that if we make firstName object-private instead of class-private, it works exactly like a plain old keyword-less constructor parameter:

class Person(private[this] val firstName: String, lastName: String)

This works even with var instead of val:

class Person(private[this] var firstName: String, lastName: String)

Both of these classes will have no fields. See section 5.2 of the language specification for more details about object-private access.




回答2:


as a supplement, if your class is a case class, all of the constructor parameters will be automatically public fields.

The compiler will complain about the private keyword if it exists, and for the parameters without val/var, no matter they are used or not in any defs, there will be public fields generated for them.



来源:https://stackoverflow.com/questions/18623687/scala-constructor-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!