Skip to content

Instantly share code, notes, and snippets.

@RSchulz
Created January 23, 2014 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RSchulz/8581033 to your computer and use it in GitHub Desktop.
Save RSchulz/8581033 to your computer and use it in GitHub Desktop.
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