0

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]?

NietzscheanAI
  • 966
  • 6
  • 16
  • You could use macros for this, though it wouldn't be trivial and it would have to seriously justify the use case which I don't currently see. You would need to check the type bounds on generic method parameters and assert on the bound being satisfied yourself, in between a target type and a bounded generic. – flavian Jul 09 '17 at 09:37
  • @flavian - If you'd care to provide an answer with more detail on the method calls required to do this... – NietzscheanAI Jul 09 '17 at 11:02
  • I'd have to invoice you for it to do the work, it's lengthy. Learn the Macro API, I can't help you unless you're comfortable with that already. – flavian Jul 09 '17 at 11:29
  • @flavian I'm looking to do this at run time, not compile time. – NietzscheanAI Jul 16 '17 at 20:46

0 Answers0