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
lift
is a function that returns a function. The function returned lifts a value (unnamed) by applying the function f
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 function Option[A] => Option[B]
(explicitly stated), while that function has return type Option[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
Option[B]
, because f: A => B
will map type A
to B
,lift
to be Option[A] => Option[B]
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With