Created
March 14, 2014 01:44
-
-
Save markhibberd/9540729 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Accent[A] | |
final case class Zed(n: Int) extends Accent[Int] | |
final case class Zee(s: String) extends Accent[String] | |
object Example { | |
def twice[A](accent: Accent[A]): Accent[A] = accent match { | |
case Zed(n) => Zed(n + n) | |
case Zee(s) => Zee(s + s) | |
} | |
def empty[A](accent: Accent[A]): Accent[A] = accent match { | |
case Zed(n) => Zed(0) | |
case Zee(s) => Zee("") | |
} | |
/* | |
As expected you can't do this.... | |
[vex:gadt] 2048$ scalac -Ywarn-all -Xlint gadt.scala | |
gadt.scala:20: error: type mismatch; | |
found : Zee | |
required: Accent[A] | |
case Zed(n) => Zee("") | |
^ | |
gadt.scala:21: error: type mismatch; | |
found : Zed | |
required: Accent[A] | |
case Zee(s) => Zed(0) | |
^ | |
two errors found | |
*/ | |
/* | |
def notOk[A](accent: Accent[A]): Accent[A] = accent match { | |
case Zed(n) => Zee("") | |
case Zee(s) => Zed(0) | |
} | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment