Skip to content

Instantly share code, notes, and snippets.

@RSchulz
Created Jan 23, 2014
Embed
What would you like to do?
import scala.reflect.runtime.universe._
def classType[T](c: Class[T]): Type =
runtimeMirror(c.getClassLoader)
.staticClass(c.getName)
.selfType
def prodType(p: Product): Type = {
val mirror = runtimeMirror(p.getClass.getClassLoader)
val cSym = mirror.staticClass(p.getClass.getName)
cSym.selfType
}
def tagType[T <: Product : TypeTag] = typeOf[T]
case class CC1(name: String, stuff: Traversable[Any])
object O1 {
case class CC2(i: Int, x: Double)
}
val cc1 = CC1("foo", Map("one" -> 3, "two" -> 3, "three" -> 5, "four" -> 4))
val cc2 = O1.CC2(22, 2.2)
// Exiting paste mode, now interpreting.
scala> tagType[CC1]
res1: reflect.runtime.universe.Type = CC1
scala> prodType(cc1)
res2: reflect.runtime.universe.Type = $line3.iw$CC1
scala> tagType[O1.CC2]
res3: reflect.runtime.universe.Type = O1.CC2
scala> prodType(cc2)
res4: reflect.runtime.universe.Type = $line3.iw$O1$CC2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment