I have following structure:
trait Runner {
def run: Unit
}
trait LoggableRunner extends Runner {
abstract override def run {
println("logging enter")
super.run
println("logging exit")
}
}
class RealRunner extends Runner {
def run = println("running...")
}
This way I can enrich my class with logging in the following way:
val a = new RealRunner with LoggableRunner
Which adds log info before and after running the run method.
Now what I really want to have is to be able to compose scala object with traits. I have tried the same way:
object RealRunner extends LoggableRunner{
def run = println("running...")
}
But I get: method run needs override modifier
So I tried:
object RealRunner extends LoggableRunner{
override def run = println("running...")
}
But I get: method run needs abstract override modifiers. So I tried again with adding abstract and I got: abstract override modifier only allowed for members of traits.
Is mixing traits into objects even possible?
class RealRunner extends Runner { def print = {...} }, and then:object RealRunner extends RealRunner with LoggableRunner. - DimaLoggableRunnertrait is extending the object, I cannot use anything thatLoggableRunnercould provide me in my print method - imagine e.g. definingval logLevel = "ERR"in LoggableRunner - how could I access it in my print method? - NNamed