Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala - No TypeTag Available Exception when using case class to try to get TypeTag?

Tags:

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.

like image 756
JasonG Avatar asked Jun 07 '13 16:06

JasonG


1 Answers

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] } 
like image 139
JasonG Avatar answered Oct 02 '22 12:10

JasonG