I have implicit classes with nested type parameters to provide relevant methods to the data-object only when certain conditions are met and to store type information for later use. Somehow a nested type parameter only works with a wildcard but not with a type parameter. I do not understand why... The following piece of code works:
trait A
trait AB extends A
trait AC extends A
trait ByA[T, AX <: A]
case class ByAX[T, AX <: A]() extends ByA[T, AX]
case class Wrapper[T]()
implicit class WithByAB[T](tpe: Wrapper[_ <: ByA[T, AB]]) {
def printsomething(): Unit = {
println("WithByAB")
}
}
Wrapper[ByAX[String, AB]]().printsomething()
//Wrapper[ByAX[String, AC]]().printsomething() does not compile (expected)
This one does not:
trait A
trait AB extends A
trait AC extends A
trait ByA[T, AX <: A]
case class ByAX[T, AX <: A]() extends ByA[T, AX]
case class Wrapper[T]()
implicit class WithByAB[T, S <: ByA[T, AB]](tpe: Wrapper[S]) {
def printsomething(): Unit = {
println("WithByAB")
}
}
Wrapper[ByAX[String, AB]]().printsomething()
//Wrapper[ByAX[String, AC]]().printsomething() does not compile (expected)
It looks like the compiler cannot resolve type T but if the first example works and preserves the parameter-type information, why not in the second example?!