Skip to content

Instantly share code, notes, and snippets.

@xeno-by
Created February 19, 2013 13:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeno-by/4985929 to your computer and use it in GitHub Desktop.
Save xeno-by/4985929 to your computer and use it in GitHub Desktop.
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}
trait Base {
def companion: MetaBase = {
val classSymbol = cm.classSymbol(getClass)
val moduleSymbol = classSymbol.companionSymbol.asModule
val moduleMirror = cm.reflectModule(moduleSymbol)
moduleMirror.instance.asInstanceOf[MetaBase]
}
}
trait MetaBase {
// stuff
}
object Test extends App {
class Foo extends Base
object Foo extends MetaBase
println(new Foo().companion)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment