Last active
February 4, 2016 04:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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