I am struggling to obtained the types of the arguments of a defined function in Scala. For example Funcion1[T1, T2]
.
Since Java will eliminate the types checking (compiler warning: is unchecked since it is eliminated by erasure
), I would like to find a way to match
that function with their types.
The goal is to be able to have same functionality as:
val fnInput = {x: Map[String, Double] => x}
fnInput match {
case f: Function1[Map[String, Double], Map[String, Double]] => ???
case f: Function1[T1, T2] => ???
case f: Function2[T1, T2, T3] => ???
}
But, checking the arguments types.
Updated: so far my solution will go into using the following tools
import scala.reflect.runtime.universe._
def getType[T: TypeTag](obj: T) = typeOf[T]
val t = getType({x: Map[String, Any] => x})
// check first argument
typeOf[Map[String, Int]] <:< t.typeArgs(0)
// check return of Function1
typeOf[Map[String, Int]] <:< t.typeArgs(1)
// t.typeArgs.length will return the number of arguments +1
Do you believe that this is a good approach?