The following code compiles with no warning but does not work as intended.
inline def isXAssignableToY[X <: Matchable, Y]: Boolean = {
inline erasedValue[X] match {
case _: Y => true
case _ => false
}
}
def isSerializable[A <: Matchable]: Boolean = isXAssignableToY[A, Serializable]
def isSuperOfString[B]: Boolean = isXAssignableToY[String, B]
@main def test19058(): Unit = {
val a = isXAssignableToY[String, Serializable]
println(a) // prints true as intended
val b = isSerializable[String]
println(b) // prints false, not as intended
val c = isSuperOfString[Serializable]
println(c) // prints false, not as intended
}
The methods isSerializable
and isSuperOfString
don’t do what is intended because they pass an abstract type parameter to isXAssignableToY
.
How can I constraint the isXAssignableToY
method usage such that the compiler complains when the type arguments are abstract type parameters?
This question is related to this other question: Misleading unreachable warning when a type pattern match that is the result of a inline expansion is involved?