问题
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.)
回答1:
You can try
class A {
type T
}
class B extends A {
class T
}
val b: B = new B
val t: b.T = new b.T
I made T
a type member rather than type parameter.
Here a class overrides a type.
If you want to use T
also as a type parameter you can introduce Aux-type
object A {
type Aux[_T] = A { type T = _T }
}
and use type A.Aux[T]
as a replacement to your original A[T]
.
Now you're doing
trait A[X]
class B {
class T
/*implicit*/ object typeclass extends A[T]
}
val b = new B
implicit val b_typeclass: b.typeclass.type = b.typeclass
val b1 = new B
implicit val b1_typeclass: b1.typeclass.type = b1.typeclass
implicitly[A[b.T]]
implicitly[A[b1.T]]
Please notice that making object typeclass
implicit is now useless.
Making object typeclass
implicit would be useful if you made b
, b1
objects rather than values
trait A[X]
class B {
class T
implicit object typeclass extends A[T]
}
object b extends B
object b1 extends B
implicitly[A[b.T]]
implicitly[A[b1.T]]
If you want to make B
extend A
then as earlier I propose to replace type parameter of A
with a type member. A
will still be a type class, just type-member type class rather than type-parameter type class
trait A {
type X
}
object A {
type Aux[_X] = A {type X = _X}
}
class B extends A {
type X = T
class T
}
implicit object b extends B
implicit object b1 extends B
implicitly[A.Aux[b.T]]
implicitly[A.Aux[b1.T]]
来源:https://stackoverflow.com/questions/64104900/inherit-from-a-class-parametrized-by-an-inner-type