I have an enormous class that I want to break in many traits. Each part of my class has a set of vals not used by the other parts. These parameters are read from a property file, sometimes with some calculation. I would like to have each trait be responsible for initializing its own variables. I would like these values to be private to the trait. Unfortunately, traits do not have constructors (which is what I really want).
What is the pattern for creating an object that mixes a set of traits, where the traits have values that need initializing?
Here is an example:
class Foo( properties: Properties ) extends Bar with Baz
trait Bar {
private val something
}
trait Baz {
private val somethingElse
}
How do I initialize Bar.something and Baz.somethingElse without either making them abstract and non-private, or adding an init() method and making them vars?
Thank you Peter
What about early initialization?
trait A {
val f: String
val a = "I would have used " + f
}
trait B {
val f: String
val b = "I would have used " + f + " too!"
}
class C extends { val f = "the string" } with A with B
If you throw this into a REPL:
scala> List(c.a, c.b).map(println _)
I would have used the string
I would have used the string too!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With