Skip to content

Instantly share code, notes, and snippets.

@lildata
Forked from piotrga/gist:5928581
Last active August 29, 2015 14:21
Show Gist options
  • Save lildata/4781b5c8e30e7876ff71 to your computer and use it in GitHub Desktop.
Save lildata/4781b5c8e30e7876ff71 to your computer and use it in GitHub Desktop.
Get companion object instance with new Scala reflection API
trait ReflectionSugars{
import scala.reflect.runtime.{universe => ru}
private lazy val universeMirror = ru.runtimeMirror(getClass.getClassLoader)
def companionOf[T](implicit tt: ru.TypeTag[T]) = {
val companionMirror = universeMirror.reflectModule(ru.typeOf[T].typeSymbol.companionSymbol.asModule)
companionMirror.instance
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment