I’m using a Logging trait in my application and I’m curious whether it’s possible to access a protected variable from the Logging trait.
This is what I have:
class MyClass extends ExternalTrait with Logging
trait ExternalTrait {
protected val protectedVar = "secret?"
}
trait Logging {
if(this.isInstanceOf[OtherTrait])
this.asInstanceOf[OtherTrait].protectedVar
}
but the access to the protected variable is restricted when accessing in this manner. Is there some other way to access the protectedVar from the Logging trait?
Many thanks.
If you definitely know that
Logginglater is mixed in toExternalTraityou can place a self reference:Of course, if there can be logging traits which do not extend/mixin other traits self references are not suitable. In such a case I would subclass
Loggingto handle the different behavior.