Reputation: 4242
scala> def lift3[A,B,C,D] (
| f: Function3[A,B,C,D]): Function3[Option[A], Option[B], Option[C], Option
[D]] = {
| (oa: Option[A], ob:Option[B], oc: Option[C]) =>
| for(a <- oa; b <- ob; c <- oc) yield f(a,b,c)
| }
lift3: [A, B, C, D](f: (A, B, C) => D)(Option[A], Option[B], Option[C]) => Option[D]
In particular, the following line:
def lift3[A,B,C,D] (
f: Function3[A,B,C,D]): Function3[Option[A], Option[B], Option[C], Option
[D]]
This is taken from the book Scala In Depth by Joshua D Suereth (Listing 2.1, chapter 2). I'm not sure what purpose the additional Option[D]
serves. In the body of the function, the code only maps to the first three parameters to the output type D
. When then, D
is declared in the input parameter list? Am I missing something?
With my limited understanding, I would read the function declaration as a function that takes a function as a parameter (which in turn takes 4 parameters) and returns a function that takes 4 parameters. Also, why is there no mention of the return type? Thanks in advance!
Upvotes: 2
Views: 105
Reputation: 23532
A function3
takes 3 parameters. The D
is the return type of the function. What the code does, is take a function with 3 arguments and return a function with 3 arguments, where each argument and its return type is "lifted" to an Option
.
You can check out the API docs for Function3
here.
And an explanation for the R
generic type can be found in the docs for Function2
Upvotes: 2
Reputation: 170859
With my limited understanding, I would read the function declaration as a function that takes a function as a parameter (which in turn takes 4 parameters) and returns a function that takes 4 parameters. Also, why is there no mention of the return type?
Function3[A,B,C,D]
is a function with 3 parameters (of types A
, B
and C
) and D
is the return type (it can also be written as (A, B, C) => D
; this is exactly the same type). So in Function3[Option[A], Option[B], Option[C], Option[D]]
, Option[D]
is the return type, not a parameter type.
Upvotes: 3