How do I inherit generic type from parent mixin type? For example I have a trait Foo
with one generic type A
:
trait Foo[A] {
def value: A
}
I have a class User
that uses Foo[String]
, like:
class User extends Foo[String] {
override def value: String = ???
}
Everything works fine. Now, I want to add a trait Bar[A]
with self-type of Foo[A]
.
trait Bar[A] { self: Foo[A] =>
def anotherValue: A
}
If I want to use Bar
in User
, I'll need to do:
class User extends Foo[String] with Bar[String] {
override def value: String = ???
override def anotherValue: String = ???
}
Is there anyway that I can simplify User
to this? (Bar
automatically infers type from corresponding Foo
.)
class User extends Foo[String] with Bar
You can define intermediate trait
You can define a macro annotation but this would be an overkill
This would be more flexible if
A
were a type member rather than type parameter