Skip to content

Instantly share code, notes, and snippets.

@markhibberd
Created March 14, 2014 01:44
Show Gist options
  • Save markhibberd/9540729 to your computer and use it in GitHub Desktop.
Save markhibberd/9540729 to your computer and use it in GitHub Desktop.
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