Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the disadvantages of making <code>RList</code> an inner trait of a producer is that it becomes less pleasant to write methods or functions with <code>RList</code> arguments outside of the producer—you end up with a lot of this:</p> <pre><code>def foo[P &lt;: RListProducer, T](rl: P#RList[T]) = ??? </code></pre> <p>An alternative is to give <code>RList</code> a type member that will be unique for each "source" <code>RList</code>, but that each source will pass on to its "children". Something like this:</p> <pre><code>trait RList[T] { outer =&gt; type S protected val wrapped: List[T] def map[V](f: T =&gt; V) = new RList[V] { type S = outer.S protected val wrapped = outer.wrapped.map(f) } def zip[V](r: RList[V] { type S = outer.S }) = new RList[(T, V)] { type S = outer.S protected val wrapped = outer.wrapped.zip(r.wrapped) } } object RList { def toRList[T](ts: List[T]) = new RList[T] { type S = this.type protected val wrapped = ts } } </code></pre> <p>Now assume we have the following:</p> <pre><code>val a = RList.toRList(1 :: 2 :: 3 :: Nil) val b = a.map(_.toString) val c = RList.toRList("1" :: "2" :: "3" :: Nil) </code></pre> <p>Now <code>a zip b</code> (or <code>a zip b zip a zip a</code>, etc.) will compile, but if you throw in a <code>c</code> you'll get a compiler error.</p> <hr> <p><strong>Note:</strong> I'd originally written <code>zip</code> as follows:</p> <pre><code>def zip[V](r: RList[V])(implicit ev: r.S =:= S) = new RList[(T, V)] { ... } </code></pre> <p>This gives a slightly nicer compiler error message, but if you're working pre-2.10 it requires turning on dependent method types with <code>-Ydependent-method-types</code>.</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