How to initialize a trait variable that is a val

拈花ヽ惹草 提交于 2019-12-20 07:08:37

问题


I have MyObject and MyTrait:

class MyObject(private val myname: String = "") extends MyTrait {

  _name = myname

  def foo(myname : String) {
   _name = myname
  }
}

trait MyTrait {

  protected var _name: String = _

  def name = _name

}

This works fine as this

val myObject = new MyObject("abc")
println(myObject.name)
myObject.foo("def")
println(myObject.name)

prints

abc
def

as expected.

Problem now is that I want MyTrait._name to be a val instead of a var. But there is no way I can manage to get this to compile. Any hints appreciated.

Regards, Oliver


回答1:


Here is an answer that uses the very latest cutting-edge naming conventions from Rex Kerr and Martin Odersky!

Read it on the scala-debate list. And you thought they sit around working on "higher kinds" and computing with unboxed ints.

There is a PR for the style changes, but this convention will have to wait a bit.

Doc Martin says: That does look promising. I have to experiment with it a little.

So be careful with this stuff; it's experimental and probably chemically unstable.

class MyObject(override protected val initialName: String = "") extends MyTrait {

  private var myName: String = initialName

  def name_=(newName: String) {
    myName = newName
  }
  override def name = myName
}

trait MyTrait {
  protected val initialName: String = "default"
  def name = initialName
}
object Test extends App {
  val myObject = new MyObject("abc")
  println(myObject.name)
  myObject.name = "def"
  println(myObject.name)
}

The style guide has a section on brevity but is itself not brief. I'm sure there are answers on SO about "prefer def over val in traits" by Daniel Sobral. And don't forget to consult the one-question FAQ when you encounter init-order problems.



来源:https://stackoverflow.com/questions/17574123/how-to-initialize-a-trait-variable-that-is-a-val

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