Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala - pass self type annotated class to child object

sorry if that's a dumb title, i don't know how to express this clearly

say i have a logging trait:

trait Logging {
    def log(s:String)
}

and then some implementation

trait PrintlnLog extends Logging {
    def log(s:String) { println(s) }
}

which i use like this

class SomeProcess { this:Logging =>
   def doSomeJunk() {
      log("starting junk")
      ...
      log("junk finished")
   }
}

i could use this class like

val p = new SomeProcess () with PrintLog
p.doSomeJunk()

now what if i have this

class SubProcess { this:Logging => 
   def doSubJunk() {
      log("starting sub junk")
      ...
      log("finished sub junk")
   }
}

class ComplexProcess { this:Logging => 
   def doMoreJunk() {
       log("starting more junk")
       val s = new SubProcess with // ??? <-- help!
       s.doSubJunk()
       log("finished more junk")
   }
}

in ComplexProcess i want to instantiate a SubProcess mixing in the same logging trait that has been mixed into ComplexProcess, but ComplexProcess doesn't know what that is. is there a way to get a reference to it?

like image 595
dvmlls Avatar asked Mar 05 '12 16:03

dvmlls


1 Answers

You cannot do that. In this case, you'd probably do something like this:

trait WithSubProcess {
  def s: SubProcess
}

class ComplexProcess { this: Logging with WithSubProcess ... }
like image 140
Daniel C. Sobral Avatar answered Oct 28 '22 11:10

Daniel C. Sobral