Skip to content

Instantly share code, notes, and snippets.

@bbarker
Created September 15, 2016 12:56
Show Gist options
  • Save bbarker/b1164c15f9a9d0330651dd5fe37fe43a to your computer and use it in GitHub Desktop.
Save bbarker/b1164c15f9a9d0330651dd5fe37fe43a to your computer and use it in GitHub Desktop.
Showing that you need to remember to add the None case to your extractor
object ExtTest {
def unapply(arg: Int): Option[Int] = arg match {
case a if a < 5 => Some(a)
}
}
val x = 3
x match {
case ExtTest(a) => println(a)
}
val y = 10
y match {
case ExtTest(a) => println(a)
// This will result in a MatchError
}
@bbarker
Copy link
Author

bbarker commented Sep 15, 2016

At least, you need to appropriately generate None, whether or not it is through a case clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment