Scala: getting the name of the class the trait is mixed in
- by Alexey Romanov
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.