I would like to have a class B
that inherits from a generic class A
that is parametrized by an inner type of B
. Specifically, I would like this (minimized example):
class A[T]
class B extends A[T] {
class T
}
Written like this, the compiler does not accept it. Is there any way to specify this inheritance relationship? (Using some different syntax, or some tricks.)
If not, what would be an official reference documenting that this is not possible?
Notes:
Yes, I want
T
to be an inner class. I do want separate instances ofB
to have incompatible typesT
.This question considers the same situation but does not ask whether/how it is possible, but instead asks for other ways to write the same thing. (At least that's how the answers seem to interpret it.) In any case, that question is ten years old, and Scala may have evolved since.
Clarification: I want
B
to inherit fromA
, not from an inner class ofA
, or have an inner class ofB
inherit fromA
. The reason is that I want to use it in the following situation:A
is a type class.B
is supposed to provide a quick way to easily generate new classes of that type class (with minimal boilerplate). I want to the user of the class to do something like:implicit val X = createBInstance()
, and then they will be able to use the typeX.T
and it will have type classA
. The best I managed so far is to support the patternval X = createBInstance(); implicit val xTypeclass = X.typeclass
and useX.T
. This means more boilerplate, the possibility to forget something, and it pollutes the namespace more (additional namexTypeclass
). (For the very curious:A
isMLValue.Converter
,B
isQuickConverter
, andX
isHeader
/RuntimeError
/... in my code.)