在构造函数/scala 中覆盖惰性 val
override lazy val in constructor / scala
我想在 scala 的构造函数中重写一个惰性 val。任何想法如何做到这一点?我尝试了以下但我得到
'lazy' modifier not allowed here, use call-by-name parameter instead
class Dog(override lazy val creatureType: String) extends Animal {
// valid
// override lazy val creatureType: String = "Dog"
// but i want to override it in the constructor directly
}
家长 class :
class Animal {
lazy val creatureType: String = "unknown"
}
像这样的东西应该可以工作:
class Dog(foo: => String) extends Animal {
override lazy val creatureType = foo
}
我想在 scala 的构造函数中重写一个惰性 val。任何想法如何做到这一点?我尝试了以下但我得到
'lazy' modifier not allowed here, use call-by-name parameter instead
class Dog(override lazy val creatureType: String) extends Animal {
// valid
// override lazy val creatureType: String = "Dog"
// but i want to override it in the constructor directly
}
家长 class :
class Animal {
lazy val creatureType: String = "unknown"
}
像这样的东西应该可以工作:
class Dog(foo: => String) extends Animal {
override lazy val creatureType = foo
}