Given a target type (say List[String]
) and some object o, the goal is to find a method of o with a return type that is compatible with the target type.
In the absence of generics, one can check this by comparing the target type and the return type of the method using the <:<
operator (the scala reflection analog of Java's isAssignableFrom
) from scala.reflect.runtime.universe
.
This approach does not work in the presence of generics: for example, the return type of the method def getEmptyList[T]: List[T] = Nil
does not satisfy List[T] <:< List[String]
. How does one determine that the return type of
getEmptyList[T]
is indeed compatible with List[String]
?