I want to create a general purpose method def getOptionalArgs[T](): List[String]
such that when passed a type, it returns the list of all arguments to that type that are an Option[_]
E.g. I have a case class Foo(bar: String, baz: Option[String])
. In this case, getOptionalArgs[Foo]()
would return List("baz")
.
I've been messing around with the reflections library, and I think I'm close via:
import scala.reflect.runtime.{universe, universe => ru}
def getOptionalArgs[T]() = {
val constructor = ru.typeOf[T].decl(ru.termNames.CONSTRUCTOR).asMethod
...
}
But I cant figure out how map/filter the reflected constructors parameters. What is missing here?
Update
constructor.paramLists.head.map(t => (t.name.toString, t.info))
will return List[(String, reflect.runtime.universe.Type)] = List((bar,Int), (baz,Option[Int]))
. So Its very close. Now I have to be able to figure out how to test equality between Type
, and no, equals
doesn't seem to work.