lift
is a function that returns a function. The function returned lifts a value (unnamed) by applying the functionf
to that value. The unnamed value to be lifted is referred to as_
. You can certainly give it a more explicit name:def lift[A,B](f: A => B): Option[A] => Option[B] = { value => value map f }
The return type of that function (the one being returned) needs to be either stated explicitly or determined implicitly. As written the compile can infer that what is to be returned is an
Option[B]
(more specifically,lift
is returning a functionOption[A] => Option[B]
(explicitly stated), while that function has return typeOption[B]
(determined implicitly)). Without that type information, the compiler needs some other indication of what the return type(s) are.Alternately, define
lift
thus:def lift[A,B](f: A => B) = { value: Option[A] => value map f }
Here you're explicitly stating the type for
value
, and the compiler can infer- the return type of the function returned to be
Option[B]
, becausef: A => B
will map typeA
toB
, - the return type for
lift
to beOption[A] => Option[B]
.
- the return type of the function returned to be
Confusion with 'lifting' functions in Scala
-
01-06-2022 - |
Question
In the book Functional Programming In Scala, there's an example of 'Lift' where a function with type A => B
is promoted to Option[A] => Option[B]
.
This is how lift is implemented:
def lift[A,B](f: A => B):Option[A] => Option[B] = _ map f
I have a couple of confusions regarding this:
The first one is, what is the '_' here? And secondly, when I remove the return type from the def, expecting the type-inference to do its magic I get the following exception:
scala> def lift[A,B](f: A => B) = _ map f
<console>:7: error: missing parameter type for expanded function ((x$1) => x$1.map(f))
def lift[A,B](f: A => B) = _ map f
Can somebody explain what's going on here?
Thanks
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow