Skip to content

Instantly share code, notes, and snippets.

@inanna-malick
Last active February 4, 2016 04:14
Show Gist options
  • Save inanna-malick/e28be9ae22eb3225fc6c to your computer and use it in GitHub Desktop.
Save inanna-malick/e28be9ae22eb3225fc6c to your computer and use it in GitHub Desktop.
{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