woggioni
woggioni

Reputation: 1432

How to get the runtime class of a generic type in Scala?

import scala.reflect.runtime.universe._
import scala.reflect.ClassTag
import scala.reflect.classTag

def getTypeTag[T: TypeTag](obj: T) = println(typeTag[T].tpe)

class Entity
{
    def greet = println("Hello!")
}

class Point[T : TypeTag](val x : T, val y :T) extends Entity

val p = new Point[Int](2,5)
val p2 = new Point[Float](1.0f,-5.0f)
val p3 = new Point[Double](4.0,-7.0)

val path = Array[Entity](p,p2,p3)

path.foreach(getTypeTag(_))

This small code snippet writes to stdout

Helper.this.Entity
Helper.this.Entity
Helper.this.Entity

I'd like it to write

Helper.this.Point[Int]
Helper.this.Point[Float]
Helper.this.Point[Double]

I know there are already a lot of question about scala reflection, I read many of them but still haven't understand the difference between TypeTag, Manifest and ClassTag; nor I can get this very basic example to work. Any help would be greatly appreciated.

Thanks in advance

Upvotes: 3

Views: 1292

Answers (1)

OlivierBlanvillain
OlivierBlanvillain

Reputation: 7768

Short, reasonable answer:

You can't. Don't do that. Why do you need that in the first place? You are probably doing it wrong...

Unsatisfactory answer:

def printType(a: Any) =
  a match {
    case p: Point[_] =>
      val clazz = p.getClass.getName
      val tname = p.x.getClass.getName
      println(s"$clazz[$tname]")
    case _ =>
      println("yolo")
  }

class Entity 

class Point[T](val x: T, val y: T) extends Entity

val p = new Point[Int](2,5)
val p2 = new Point[Float](1.0f,-5.0f)
val p3 = new Point[Double](4.0,-7.0)

val path = Array[Entity](p, p2, p3)

path.foreach(printType)

Actual answer:

Use shapeless Typeable.

import shapeless._

val path = p :: p2 :: p3 :: HNil

object printType extends Poly1 {
  implicit def case0[T](implicit t: Typeable[T]) =
    at[T](_ => t.describe)
}

path.map(printType).toList.foreach(println)

Upvotes: 1

Related Questions