Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Created October 3, 2012 13:01
Show Gist options
  • Save Blaisorblade/3826781 to your computer and use it in GitHub Desktop.
Save Blaisorblade/3826781 to your computer and use it in GitHub Desktop.
//Paste this in the REPL
import language.implicitConversions
trait Exp[+T]
trait Def[+T]
case class Eq[T](a: Exp[T], b: Exp[T]) extends Def[T]
case class Const[T](t: T) extends Exp[T]
class Eq2[T](a: Exp[T], b: Exp[T]) extends Eq[T](a, b) with Exp[T] //This can be defined before or after f1 and f2, it makes no difference.
def f1[T](e: Exp[T]) = e match {
case Eq(a, b) => 1
case _ => 0
}
def f2[T](e: Exp[T]) = e match {
case eq: Eq[u] => 1
case _ => 0
}
f2(new Eq2(Const(1), Const("Foo")))
//If I actually want to write f2, maybe it's better to make the intent explicit and write:
def f3[T](e: Exp[T]) = e match {
case eq: Eq[u] with Exp[b] => 1
case _ => 0
}
$ ../scala-2.10.0-M7/bin/scala -Xlint -feature
Welcome to Scala version 2.10.0-M7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import language.implicitConversions
import language.implicitConversions
scala>
scala> trait Exp[+T]
defined trait Exp
scala> trait Def[+T]
defined trait Def
scala>
scala> case class Eq[T](a: Exp[T], b: Exp[T]) extends Def[T]
defined class Eq
scala> case class Const[T](t: T) extends Exp[T]
defined class Const
scala> class Eq2[T](a: Exp[T], b: Exp[T]) extends Eq[T](a, b) with Exp[T]
defined class Eq2
scala>
scala> def f1[T](e: Exp[T]) = e match {
| case Eq(a, b) => 1
| case _ => 0
| }
<console>:13: error: constructor cannot be instantiated to expected type;
found : Eq[T]
required: Exp[?T1] where type ?T1 <: T (this is a GADT skolem)
case Eq(a, b) => 1
^
scala> def f2[T](e: Exp[T]) = e match {
| case eq: Eq[u] => 1
| case _ => 0
| }
f2: [T](e: Exp[T])Int
scala>
scala> f2(new Eq2(Const(1), Const("Foo")))
res0: Int = 1
scala>
scala> def f3[T](e: Exp[T]) = e match {
| case eq: Eq[u] with Exp[b] => 1
| case _ => 0
| }
f3: [T](e: Exp[T])Int
scala>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment