Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer given by Kevin Wright is entirely correct, but to break it down further:</p> <p>The first line is declaring a function called <code>foo</code>. <code>foo</code> takes as its argument another function, <code>bar</code> that itself takes in <code>Unit</code> and returns a <code>String</code>. Generally speaking, <code>Unit</code> in scala has the same meaning that <code>void</code> does in many other languages, so you could say for the most part that <code>bar</code> is a function that takes in no arguments and returns a <code>String</code>.</p> <p>The body of the <code>foo</code> function simply returns the argument it received. Therefore, scala infers that <code>foo</code> returns a function that takes <code>Unit</code> and returns a <code>String</code>.</p> <p>The second command calls <code>foo</code> with the function <code>a =&gt; a.toString</code> as its argument. <code>a</code> is assumed to be of type <code>Unit</code>. If <code>Unit</code> was an exact analogue to <code>void</code>, this wouldn't work. You can't call <code>toString</code> on the absence of something. However, <code>Unit</code> behaves slightly differently, exactly for situations like this, and <code>a</code> will be given an instance of <code>Unit</code>. This instance won't really be able to do much, but it will be able to have <code>toString</code> called on it. So the result of the second command will be a function that returns the result of <code>toString</code> called on the <code>Unit</code> instance, which is: "()"</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. 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