Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Flaviu's answer inspired me to try playing with implicits. The idea is to see if the trace looks better with the 'trace' further to the right on the line:</p> <pre><code>import Trace._ object Main { def main(args:Array[String]) { val listList = List(List(1,2,3), List(3,4)) for { list &lt;- trace1(listList, "lList is: %s", listList) // trace() item &lt;- list traced("list is: %s", list) // implicit } yield item </code></pre> <p>I also wanted to try mixing in error logging in the same comprehension. Error logging seems to look best mixed with Daniel's approach: </p> <pre><code> val optOpt:Option[Option[Int]] = Some(Some(1)) for { opt &lt;- optOpt; _ = trace2("opt found: %s", opt) // trying Daniel's suggestion int &lt;- opt orElse err("num not found in: %s", opt) // together with error logging } yield int } } </code></pre> <p>Here's the supporting code for both experiments:</p> <pre><code>object Trace { def trace1[T](any:T, message:String, params:AnyRef*):T = { Console println String.format("TRA: " + message, params:_*) any } def trace2[T](message:String, params:AnyRef*) { Console println String.format("TRA: " + message, params:_*) } def err[T](message:String, params:AnyRef*):Option[T] = { Console println String.format("ERR: " + message, params:_*) None } implicit def anyRefToTraceable[T](anyRef:T):Traceable[T] = { new Traceable(anyRef) } class Traceable[T](val self:T) { def traced(message:String, params:AnyRef*):T = { Console println String.format("TRA: " + message, params:_*) self } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload