Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The ones I can think of are</p> <h3>Existential types</h3> <pre><code>def foo(l: List[Option[_]]) = ... </code></pre> <h3>Higher kinded type parameters</h3> <pre><code>case class A[K[_],T](a: K[T]) </code></pre> <h3>Ignored variables</h3> <pre><code>val _ = 5 </code></pre> <h3>Ignored parameters</h3> <pre><code>List(1, 2, 3) foreach { _ =&gt; println("Hi") } </code></pre> <h3>Ignored names of self types</h3> <pre><code>trait MySeq { _: Seq[_] =&gt; } </code></pre> <h3>Wildcard patterns</h3> <pre><code>Some(5) match { case Some(_) =&gt; println("Yes") } </code></pre> <h3>Wildcard imports</h3> <pre><code>import java.util._ </code></pre> <h3>Hiding imports</h3> <pre><code>import java.util.{ArrayList =&gt; _, _} </code></pre> <h3>Joining letters to punctuation</h3> <pre><code>def bang_!(x: Int) = 5 </code></pre> <h3>Assignment operators</h3> <pre><code>def foo_=(x: Int) { ... } </code></pre> <h3>Placeholder syntax</h3> <pre><code>List(1, 2, 3) map (_ + 2) </code></pre> <h3>Partially applied functions</h3> <pre><code>List(1, 2, 3) foreach println _ </code></pre> <h3>Converting call-by-name parameters to functions</h3> <pre><code>def toFunction(callByName: =&gt; Int): () =&gt; Int = callByName _ </code></pre> <p>There may be others I have forgotten!</p> <hr> <p>Example showing why <code>foo(_)</code> and <code>foo _</code> are different:</p> <p>This example <a href="https://stackoverflow.com/questions/9610736/strange-type-mismatch-when-using-member-access-instead-of-extractor/9610961">comes from 0__</a>:</p> <pre><code>trait PlaceholderExample { def process[A](f: A =&gt; Unit) val set: Set[_ =&gt; Unit] set.foreach(process _) // Error set.foreach(process(_)) // No Error } </code></pre> <p>In the first case, <code>process _</code> represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no <em>type</em> that can be filled in for <code>A</code> that will give the type <code>(_ =&gt; Unit) =&gt; ?</code> (Existential <code>_</code> is not a type).</p> <p>In the second case, <code>process(_)</code> is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that <code>foreach</code> expects, and <code>_ =&gt; Unit</code> <em>is</em> a type (whereas just plain <code>_</code> isn't), so it can be substituted and inferred.</p> <p>This may well be the trickiest gotcha in Scala I have ever encountered.</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.
 

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