Last active
November 30, 2016 18:32
-
-
Save leifwickland/9ef89b8da371d99a5ffe9893eeef3a35 to your computer and use it in GitHub Desktop.
How to get a `Class[T]`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect._ | |
class ClassTagOps[T](val t: ClassTag[T]) extends AnyVal { | |
def strongRuntimeClass = t.runtimeClass.asInstanceOf[Class[T]] | |
} | |
trait ClassTagSyntax { | |
def classTagOps[T](t: ClassTag[T]): ClassTagOps[T] = new ClassTagOps[T](t) | |
} | |
// Afterward you can: | |
// import ClassUtils.syntax._ | |
// implicitly[ClassTag[String]].strongRuntimeClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Credit to aluizdinizsilva | |
object ClassUtils { | |
def tagToClass[C](tag: WeakTypeTag[C]) = Class.forName(tag.tpe.baseClasses.head.asClass.fullName).asInstanceOf[Class[C]] | |
object syntax extends ClassTagSyntax | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment