Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Scala distinguishes between the following things:</p> <ul> <li>Functions/methods with <em>no parameter lists</em> ("by-name parameter" if a function)</li> <li>Functions with <em>one empty parameter list</em></li> <li>Functions with <em>one parameter of type Unit</em></li> </ul> <p>None of these are equivalent, although as a convenience Scala allows you to elide empty parameter lists. (Incidentally, two empty parameter lists are also not the same.)</p> <p>So, even though <code>Unit</code> is written <code>()</code>, this is <em>not</em> the same as the function argument parens <code>()</code> for a function or method. Instead, think of <code>()</code> as a <code>Tuple0</code>.</p> <p>So, if you say <code>f: Unit =&gt; Int</code>, what you mean is "f takes one parameter, but it's a really boring parameter because it is <code>Unit</code>, which must always be the same boring <code>Tuple0</code> value <code>()</code>". What you're writing is really short for <code>f: (Unit) =&gt; Int</code>.</p> <p>If you say <code>f: () =&gt; Int</code>, then you mean that "f takes no parameters and produces an <code>Int</code>".</p> <p>If you say <code>f: =&gt; Int</code>, then you mean that "delay the execution of whatever statement produces an <code>Int</code> value until we use it in this code (and re-evaluate it each time)". Functionally, this ends up being basically the same as <code>f: () =&gt; Int</code> (and internally is converted into the same <code>Function0</code> class), but it has a different usage, presumably to allow for a more compact form of closures (you always omit the <code>=&gt;</code> in the calling code).</p>
 

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