Note that there are some explanatory texts on larger screens.

plurals
  1. POConstrain function based on origin (Path Dependent type? Type Generation?)
    text
    copied!<p>Sorry about terrible title, not sure of a better one. Here's a gross simplification of my problem (Sorry if it seems so trivial, that it's pointless):</p> <pre><code>class RList[T](data: List[T]) { def map[V](f: T=&gt;V): RList[V] = ... } </code></pre> <p>The idea of an <code>RList</code> (Restricted List) is that you can't resize it, or change the order of the elements in it. But you can use functions that give you a new RList with changed data in it.</p> <p>Now there needs to be a function that creates RLists. It might have a signature <em>something</em> like:</p> <pre><code>def toRList[T](values: List[T]): RList[T] = ... </code></pre> <p>So far, so good. But now the tricky part. I need a function that works <em>like</em> this:</p> <pre><code>def zip[T, V](left: RList[T], right: RList[V]): RList[(T,V)] </code></pre> <p>But with the additional constraint that <code>left</code> and <code>right</code> have the same origin. Thus, they are guaranteed to be the same size.</p> <p>e.g. Code that should compile:</p> <pre><code>val x = toRList(List(1, 2, 3)) val y = x.map(_ * 2) val z = y.map(_.toString) zip(y,z) </code></pre> <p>e.g. Code that should fail <em>to compile</em></p> <pre><code>val y = toRList(List(2, 4, 6)) val z = toRList(List("one", "two")) zip(y,z) </code></pre> <p>*<em>Note: In my original problem the constraint on zip needs to be that they are from the same 'source'. Merely guaranteeing they are the same length isn't good enough (Not to mention, the size of the list isn't known at compile time) *</em></p> <p>I also need to be able to use <code>zip</code> multiple times, so something like this should compile</p> <pre><code>zip(a,zip(b,c)) </code></pre> <p>(assuming <code>a</code>, <code>b</code> and <code>c</code> are from the same source)</p> <p>Thanks!</p>
 

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