Say I have the following code:
trait Trait[T <: Trait[T]] {
def merge(t: T): T
}
case class A[T <: Trait[T]](t: T, i: Int)
case class B[T <: Trait[T]](t: T, str: String)
Is there a way I can define a type to abbreviate my definitions of classes A and B?
So something like:
type T2 = _ <: Trait[T2] // ???
case class A[T2](t: T2, i: Int)
case class B[T2](t: T2, str: String)
Actually, you don't want an alias for a type, you want an alias for a bound.
Please see How to avoid duplication of type bound in Scala
Briefly, you should keep F-bounds wherever you need them. Actually, this is not a code duplication. Type parameter
T
ofTrait
,A
,B
are actually three different type parameters, which can have different bounds.But theoretically you can abbreviate bounds with a macro annotation, although this is not worth it and generally can be dangerous, because this can be surprising for your team mates, can make debugging more complicated and can confuse your IDE
Usage: