This is the code in question. Something of a description/explination is included below
object Functions {
import scala.reflect.Manifest
private var functions: List[(Manifest[_],Any,String)] = List()
def add[T](desc: String,func: T)(implicit m: Manifest[T]) {
functions ::= (m,func,desc)
}
def get[T]()(implicit m : Manifest[T]): List[(T,String)] = {
functions flatMap {
case (t,f,s) => if (t <:< m) Some(f.asInstanceOf[T],s) else None
}
}
def getInputs[T]()(implicit m : Manifest[T]): List[(T => Any,String)] = {
functions flatMap {
case (t,f,s) => if (t <:< (T => Any)) Some(f.asInstanceOf[T => Any],s) else None
}
}
Basically my goal is to have a list of functions, with their types (input and output) perserved and the ability to search for specific functions based off of what they take as input and what they take as output. I am starting to believe that this is not possible in scala without some sort of a monkey patch, which I would like to avoid.
You should be able to do what you want with shapeless‘s
HLists,Sample REPL session,
Note that the individual types of the functions are preserved in the
HListfs, that the individual types of the argument values are preserved inargs, and that the types of the elements of the result of thezipApplyare determined appropriately by the types of the corresponding function and argument.