Scala: getting the name of the class the trait is mixed in

Posted by Alexey Romanov on Stack Overflow See other posts from Stack Overflow or by Alexey Romanov
Published on 2010-05-12T13:13:50Z Indexed on 2010/05/12 13:14 UTC
Read the original article Hit count: 210

Filed under:
|

Given an instance of a class, we can obviously return its name:

trait MixedInClassDiscovery {
  val className = this.getClass.getName
}

class AClass extends MixedInClassDiscovery {
  ...
  this.className // returns "AClass"
  ...
}

But this way uses reflection, once for every instance of AClass. Can the same be done once for every class, instead?

One solution which comes to mind is to mix it into companion objects instead of classes themselves.

© Stack Overflow or respective owner

Related posts about scala

Related posts about traits