Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's no difference between a def'ed function and a val'ed function:</p> <pre><code>scala&gt; def test1 = (str: String) =&gt; str + str test1: (String) =&gt; java.lang.String scala&gt; val test2 = test1 test2: (String) =&gt; java.lang.String = &lt;function1&gt; scala&gt; val test3 = (str: String) =&gt; str + str test3: (String) =&gt; java.lang.String = &lt;function1&gt; scala&gt; val test4 = test2 test4: (String) =&gt; java.lang.String = &lt;function1&gt; </code></pre> <p>See? All of these are functions, which is indicated by the <code>X =&gt; Y</code> type they have.</p> <pre><code>scala&gt; def test5(str: String) = str + str test5: (str: String)java.lang.String </code></pre> <p>Do you see an <code>X =&gt; Y</code> type? If you do, go see an ophthalmologist, because there's none. The type here is <code>(X)Y</code>, commonly used to denote a <em>method</em>.</p> <p>Actually, <code>test1</code>, <code>test2</code>, <code>test3</code> and <code>test4</code> are all methods, which return functions. <code>test5</code> is a method which returns a <code>java.lang.String</code>. Also, <code>test1</code> through <code>test4</code> do not take parameters (only <code>test1</code> could, anyway), while <code>test5</code> does.</p> <p>So, the difference is pretty simple. In the first case, you tried to assign a method to a val, but did not fill in the parameters the method take. So it failed, until you added a trailing underscore, which meant <em>turn my method into a function</em>.</p> <p>In the second example you had a function, so you didn't need to do anything else.</p> <p>A method is not a function, and vice versa. A function is an object of one of the <code>FunctionN</code> classes. A method is a handle to some piece of code associated with an object.</p> <p>See various questions about methods vs functions on Stack Overflow.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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