简体   繁体   中英

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

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 need type tags for nested classes, you may use WeakTypeTag. I don't know why, but that's intentionally .

Please declare Case class before you run main class

Example

case class Person(name: String, age: Long)

def main(args: Array[String]) {
   ...
    val people = sc.textFile("C:\\winutils\\people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF()

You need a TypeTag at compile time, because that is the way to work around erasure:

import scala.reflect.runtime.{currentMirror => m, universe => ru}

def getChildSettings[T: TypeTag](path: String, settingsParameterObject: T) = {
  val tpe    = ru.typeOf[T]
  // val classC = tpe.typeSymbol.asClass
  val ctor   = tpe.declaration(ru.nme.CONSTRUCTOR).asMethod
  ...
}

The Scala Documentation Overview for Reflection gives an example of getting and invoking a constructor.

Add this code in your pom.xml

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-reflect</artifactId>
    <version>2.12.0-M4</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM