Note that there are some explanatory texts on larger screens.

plurals
  1. POPattern matching for abstracted case classes
    primarykey
    data
    text
    <p>I'm trying to abstract case classes in a module using dependent method types and a nightly build of the compiler (2.10.0.r26005-b20111114020239). I found some inspiration from <a href="https://stackoverflow.com/questions/7860163/what-are-some-compelling-use-cases-for-dependent-method-types">Miles Sabin' example</a>.</p> <p>I don't really understand what's wrong in the (self-contained) code below. The output depends on the order of the patterns in <code>foo</code>.</p> <pre><code>// afaik, the compiler doesn't not expose the unapply method // for a companion object trait Isomorphic[A, B] { def apply(x: A): B def unapply(x: B): Option[A] } // abstract module trait Module { // 3 types with some contraints type X type Y &lt;: X type Z &lt;: X // and their "companion" objects def X: Isomorphic[Int, X] def Y: Isomorphic[X, Y] def Z: Isomorphic[Y, Z] } // an implementation relying on case classes object ConcreteModule extends Module { sealed trait X { val i: Int = 42 } object X extends Isomorphic[Int, X] { def apply(_s: Int): X = new X { } def unapply(x: X): Option[Int] = Some(x.i) } case class Y(x: X) extends X // I guess the compiler could do that for me object Y extends Isomorphic[X, Y] case class Z(y: Y) extends X object Z extends Isomorphic[Y, Z] } object Main { def foo(t: Module)(x: t.X): Unit = { import t._ // the output depends on the order of the first 3 lines // I'm not sure what's happening here... x match { // unchecked since it is eliminated by erasure case Y(_y) =&gt; println("y "+_y) // unchecked since it is eliminated by erasure case Z(_z) =&gt; println("z "+_z) // this one is fine case X(_x) =&gt; println("x "+_x) case xyz =&gt; println("xyz "+xyz) } } def bar(t: Module): Unit = { import t._ val x: X = X(42) val y: Y = Y(x) val z: Z = Z(y) foo(t)(x) foo(t)(y) foo(t)(z) } def main(args: Array[String]) = { // call bar with the concrete module bar(ConcreteModule) } } </code></pre> <p>Any idea?</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.
 

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