title: Scala: passing function parameter which does not have parameter
body: <p><a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">Observer pattern</a> or <a href="http://en.wikipedia.org/wiki/Callback_%28computer_programming%29" rel="nofollow">callback</a> is a widely used design pattern in Java. However, implementing callback interface with anonymous class is a real pain, so it is common in Scala that introducing implicit conversion for these callback class implementation. For example, implicit conversion for Runnable interface is:</p>
  
  <pre><code>implicit def func2Runnable[F](f: =&gt; F): Runnable =
    new Runnable {
      def run() {
        f
      }
    }
  </code></pre>
  
  <p>And suppose that there are some listener registry:</p>
  
  <pre><code>def addMyListener(m: Runnable) {
    // mock function for test
    for (i &lt;- 1 to 2) {
      m.run()
    }
  }
  </code></pre>
  
  <p>Then implicit conversion magically compacted my code:</p>
  
  <pre><code>addMyListener(println("Printed twice"))
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>When I pass multi-line code block to addMyListener(), only the last line of code is passed to it:</p>
  
  <pre><code>addMyListener {
    println("This line is printed once")
    println("Only this is printed twice")
  }
  </code></pre>
  
  <p><strong>Known workaround:</strong></p>
  
  <p>I added a parenthesis on the conversion function:</p>
  
  <pre><code>implicit def func2Runnable[F](f:() =&gt; F): Runnable =
    new Runnable {
      def run() {
        f()
      }
    }
  </code></pre>
  
  <p>But it is verbose when we use it:</p>
  
  <pre><code>addMyListener(() =&gt; println("Printed twice"))
  addMyListener {() =&gt;
      println("This line is printed twice")
      println("This is also printed twice")
  }
  </code></pre>
  
  <p>Is there a cleaner solution to expose this?</p>