Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>First case</strong>,</p> <pre><code>save(throw new RuntimeException("boom!")) _ </code></pre> <p>According to <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf" rel="nofollow">"Scala Reference"</a> (§6.7), trailing underscore is used in place of the argument list, and expression is converted to</p> <pre><code>val f: (Boolean) =&gt; Unit = save(throw new RuntimeException("boom!")) </code></pre> <p>where the first argument of <code>def save</code> is immediately evaluated.</p> <blockquote> <p>The expression e _ is well-formed if e is of method type or if e is a call-by-name parameter. If e is a method with parameters, e _ represents e converted to a function type by eta expansion (§6.26.5). If e is a parameterless method or call-by-name parameter of type =>T , e _ represents the function of type () => T , which evaluates e when it is applied to the empty parameterlist ().</p> </blockquote> <p>To make the things work as you expect, some modifications are required:</p> <pre><code>scala&gt; def save(f:() =&gt; Any)(run:Boolean) { if (run) { println("running f"); f() } else println("not running f") } save: (f: () =&gt; Any)(run: Boolean)Unit scala&gt; val f = save(() =&gt; throw new RuntimeException("boom!")) _ f: (Boolean) =&gt; Unit = &lt;function1&gt; scala&gt; f(true) running f java.lang.RuntimeException: boom! at $anonfun$1.apply(&lt;console&gt;:6) </code></pre> <p><strong>Second case</strong>,</p> <pre><code>save(throw new RuntimeException("boom!"))(_) </code></pre> <p>According to <a href="http://www.scala-lang.org/docu/files/ScalaReference.pdf" rel="nofollow">"Scala Reference"</a> (§6.23), when placeholder is used as a replacement for an argument, the expression is converted to</p> <pre><code>val f: (Boolean) =&gt; Unit = save(throw new RuntimeException("boom!"))(_) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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