Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{n => n + 1} and {case n => n +1} are almost but not quite the same thing
scala> val f: Function[Int, Int] = { n => n + 1 }
f: Function[Int,Int] = <function1>
scala> val f: Function[Int, Int] = { case n => n + 1 }
f: Function[Int,Int] = <function1>
scala> val pf: PartialFunction[Int, Int] = { n => n + 1 }
<console>:10: error: type mismatch;
found : Int => Int
required: PartialFunction[Int,Int]
val pf: PartialFunction[Int, Int] = { n => n + 1 }
^
scala> val pf: PartialFunction[Int, Int] = { case n => n + 1 }
pf: PartialFunction[Int,Int] = <function1>
// {n => n + 1} is a function
// {case n => n + 1} is a partial function, even though it only has one case that always matches
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.