I'm looking at the scala reflect API and I'm getting lots of exceptions.
Doc reference: http://docs.scala-lang.org/overviews/reflection/environment-universes-mirrors.html
How do I get the typetag from a generic?
def getChildSettings[T: ru.TypeTag](path: String, settingsParameterObject: T) = { import scala.reflect.runtime.{ currentMirror => m } val m = ru.runtimeMirror(getClass.getClassLoader) val classC = ru.typeOf[T].typeSymbol.asClass }
I get an exception:
No TypeTag available for ParameterObject.type
Even a very simple example doesn't seem to work (edit yes it does in the repl)
import scala.reflect.runtime.universe._ import scala.reflect.runtime.currentMirror import scala.reflect.runtime.{universe => ru} def getTypeTag[T: ru.TypeTag](obj: T) = ru.typeTag[T] case class ParameterObject(stringType: String, optionType: Option[String]) getTypeTag(ParameterObject)
I'm guessing it's something about how I'm invoking the method.
I finally found out what the issue was. The case classes must be defined top level - they cannot be nested. Something like this would fail.
class Foo { describe("getSettings") { case class ParameterObject(foo: String) settings.getTypeTag(path, ParameterObject) } } class Clazzy { def getTypeTag[T: TypeTag](obj: T) = ru.typeTag[T] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With