I am trying to expose a class parametrized with a type alias:
trait Tr[T] {
var x: T = _
}
abstract class Foo {
type MyParameter
class SomeClass extends SomeOtherClass with Tr[MyParameter]
}
class Bar extends Foo {
override type MyParameter = Int
val myObj = new SomeClass
myObj.x = 6
}
class Qux extends Foo {
override type MyParameter = String
val myObj = new SomeClass
myObj.x = "hello"
}
This works fine.
Now, what I would like to do is to specify a default value for the MyParameter type. Like this:
abstract class Foo {
type MyParameter = String
// ...
}
// ...
class Qux extends Foo {
val myObj = new SomeClass
myObj.x = "hello"
}
If I do this, however, class Bar will fail with a type error -- apparently, at that point, SomeClass is already fixed to String.
What can I do to fix this? Alternately, what other approach can I take to have SomeClass parametrized by a type that has a default, but can be overriden in subclasses of Foo?
Note that SomeClass will be more complex than shown, so I don't want the users to have to override the definition of SomeClass. Also, all uses of X.SomeClass should use the same Tr[T], so I don't want to make SomeClass itself parametrizable.