Note that there are some explanatory texts on larger screens.

plurals
  1. POScala 2.8 CanBuildFrom
    primarykey
    data
    text
    <p>Following on from another question I asked, <em><a href="https://stackoverflow.com/questions/1715681/scala-2-8-breakout">Scala 2.8 breakout</a></em>, I wanted to understand a bit more about the Scala method <code>TraversableLike[A].map</code> whose signature is as follows:</p> <pre><code>def map[B, That](f: A =&gt; B)(implicit bf: CanBuildFrom[Repr, B, That]): That </code></pre> <p>Notice a few things about this method:</p> <ul> <li>It takes a function turning each <code>A</code> in the traversable into a <code>B</code>.</li> <li>It returns <code>That</code> and takes an implicit argument of type <code>CanBuildFrom[Repr, B, That]</code>.</li> </ul> <p>I can call this as follows:</p> <pre><code>&gt; val s: Set[Int] = List("Paris", "London").map(_.length) s: Set[Int] Set(5,6) </code></pre> <p>What <strong>I cannot quite grasp</strong> is how the fact that <code>That</code> is <em>bound</em> to <code>B</code> (that is, it is some collection of B's) is being enforced by the compiler. The type parameters look to be independent of both the signature above and of the signature of the trait <code>CanBuildFrom</code> itself:</p> <pre><code>trait CanBuildFrom[-From, -Elem, +To] </code></pre> <p>How is the Scala compiler ensuring that <code>That</code> cannot be <em>forced</em> into something which does not make sense? </p> <pre><code>&gt; val s: Set[String] = List("Paris", "London").map(_.length) //will not compile </code></pre> <p>How does the compiler decide what implicit <code>CanBuildFrom</code> objects are in scope for the call?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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