Skip to content

Instantly share code, notes, and snippets.

@nicktelford
Created March 26, 2012 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicktelford/2205715 to your computer and use it in GitHub Desktop.
Save nicktelford/2205715 to your computer and use it in GitHub Desktop.
Pattern matching - why is it implemented at the language level?
object Any {
implicit def matching[A](a: A): Matchable[A] = new Matchable(a)
sealed class Matchable[A](o: A) {
// "match" is a reserved word, so lets call it "matchIt" instead
def matchIt[B](pf: PartialFunction[A, B]): B = {
if(pf.isDefinedAt(o)) {
pf(o)
} else {
throw new MatchError("...")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment