Consider this simple class:
class MyClass(p: (String, String) *) {
val params: Map[String, String] = p.toMap
}
And a class that extends it:
class SomeOtherClass(p: (String, String) *) extends MyClass(p: _*)
I would like to remove the constructor from MyClass
to avoid carrying around the initialization parameters in everything that extends it (potentially many types). Instead, I'd prefer the companion objects of the types that extend MyClass
to inherit some sort of builder method.
class MyClass {
val param = Map.empty[String, String] // some member with a default value
}
trait MyClassBuilder[A <: MyClass] {
def apply(p: (String, String) *): A = ???
}
Basically apply
should do something like this:
new A {
override val param = p.toMap
}
Obviously, the above code cannot work. The idea is that a sub-type would be used like this:
class SomeOtherClass extends MyClass
object SomeOtherClass extends MyClassBuilder[SomeOtherClass] {
// object specific things..
}
Then SomeOtherClass
would rely on the inherited apply
method for creating instances of itself. It seems something like this may be possible with reflection or macros, but I really have no idea.
Aucun commentaire:
Enregistrer un commentaire