Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>At the language level, there are only two concepts,</p> <ul> <li><p><em>Methods</em> are fundamental building blocks of Scala. Methods are <em>always</em> named. Methods live in classes or traits. Methods are a construct native to the JVM, and thus are the same in both Scala and Java. Methods in Scala (unlike functions) may have special features: they can be abstracted over type parameters, their arguments can have default values or be implicit, etc.</p></li> <li><p><em>Function objects</em> are just instances of a function trait (<a href="http://www.scala-lang.org/api/current/index.html#scala.Function1" rel="nofollow noreferrer"><code>Function1</code></a>, <a href="http://www.scala-lang.org/api/current/index.html#scala.Function2" rel="nofollow noreferrer"><code>Function2</code></a>, ...). The function is evaluated when the <code>apply</code> method on the function object is called. There is special syntax for defining unnamed "anonymous" functions (aka, "function literals"). A function is just a value, and as such can be named (e.g., <code>val f: (Int =&gt; Int) = (x =&gt; x)</code>). The type <code>A =&gt; B</code> is shorthand for <code>Function1[A, B]</code>.</p></li> </ul> <p>In the <a href="https://stackoverflow.com/questions/2529184/difference-between-method-and-function-in-scala">linked SO question</a>, it was mentioned that some references (like the Scala spec) use the word "function" imprecisely to mean either "method" or "function object". I guess part of the reason is that methods can be automatically converted to function objects, depending on the context. Note, however, that the opposite conversion wouldn't make sense: a method is not a first-class value that lives on the heap with its own independent existence. Rather, a method is inextricably linked to the class in which it is defined.</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