Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll address only number 4 here.</p> <p>One of the things that distinguishes Java "closures" from closures found in other languages is that they can be used in place of interface that does not describe a function -- for example, <code>Runnable</code>. This is what is meant by SAM, Single Abstract Method.</p> <p>Java does this because these interfaces abound in Java library, and they abound in Java library <em>because</em> Java was created without function types or closures. In their absence, every code that needed inversion of control had to resort to using a SAM interface. </p> <p>For example, <code>Arrays.sort</code> takes a <code>Comparator</code> object that will perform comparison between members of the array to be sorted. By contrast, Scala can sort a <code>List[A]</code> by receiving a function <code>(A, A) =&gt; Int</code>, which is easily passed through a closure. See note 1 at the end, however.</p> <p>So, because Scala's library was created for a language with function types and closures, there isn't need to support such a thing as SAM closures in Scala.</p> <p>Of course, there's a question of Scala/Java interoperability -- while Scala's library might not need something like SAM, <em>Java</em> library does. There are two ways that can be solved. First, because Scala supports closures and function types, it is very easy to create helper methods. For example:</p> <pre><code>def runnable(f: () =&gt; Unit) = new Runnable { def run() = f() } runnable { () =&gt; println("Hello") } // creates a Runnable </code></pre> <p>Actually, this particular example can be made even shorter by use of Scala's by-name parameters, but that's beside the point. Anyway, this is something that, arguably, Java could have done instead of what it is going to do. Given the prevalence of SAM interfaces, it is not all that surprising.</p> <p>The other way Scala handles this is through implicit conversions. By just prepending <code>implicit</code> to the <code>runnable</code> method above, one creates a method that gets automatically (note 2) applied whenever a <code>Runnable</code> is required but a function <code>() =&gt; Unit</code> is provided.</p> <p>Implicits are very unique, however, and still controversial to some extent.</p> <p><strong>Note 1</strong>: Actually, this particular example was choose with some malice... <code>Comparator</code> has <em>two</em> abstract methods instead of one, which is the whole problem with it. Since one of its methods can be implemented in terms of the other, I think they'll just "subtract" defender methods from the abstract list.</p> <p>And, on the Scala side, even though there's a sort method that uses <code>(A, A) =&gt; Boolean</code>, not <code>(A, A) =&gt; Int</code>, the standard sorting method calls for a <code>Ordering</code> object, which is quite similar to Java's <code>Comparator</code>! In Scala's case, though, <code>Ordering</code> performs the role of a <em>type class</em>.</p> <p><strong>Note 2</strong>: Implicits are automatically applied, <em>once they have been imported into scope</em>.</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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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