Reputation: 565
def multi_fun(i:Int, s:String)(d:Double) […]
How to pass this function now to another function as an argument, i.e. which parameter type needs to be indicated?
def fun_that_likes_multi_fun(mf:(Int, String)(Double) ⇒ Unit) […]
This would not work, fails even to parse.
Upvotes: 9
Views: 8277
Reputation: 10882
Just like this:
def multiFun(i:Int, s:String)(d:Double):Unit = ???
def highOrderFun(mF:(Int, String) => Double => Unit) = ???
highOrderFun(multiFun)
Think of multiFun
as of function that takes (Int, String)
and returns function with type Double => Unit
.
Upd: Regarding implicit parameters. It seems that it's not possible to pass a method with implicit parameters as is. See some details here. Although I can see nice workaround for your case:
def multiFun(i:Int, s:String)(implicit d:Double):Unit = ???
def highOrderFun(mF:(Int, String, Double) => Unit) = ???
highOrderFun(multiFun(_, _)(_))
This syntax multiFun(_, _)(_)
just creates wrapper function around multiFun
that takes three parameters whose types are inferred from params of multiFun
.
UPD: In response to @ackratos:
It appears that there is no way to make lambda function type generic. Mainly because there is a fundamental difference between lambda functions and methods.
But as functions above are defined as methods, nothing prevents us from making them generic:
def multiFun[T](i:T, s:String):Unit = ???
def highOrderFun[T](mF:(T, String) => Unit) = ???
highOrderFun(multiFun[Int])
It's possible to reference function that has implicit param list as function without one. In this case missing implicit params will be substituted by resolved values:
def multiFun(s:String)(implicit i:Int):Unit = ???
def highOrderFun(context:Any)(mF:String => Unit)(implicit i:Int) = ???
implicit val param = 2
highOrderFun(1.0)(multiFun) // param i:Int is resolved here
Upvotes: 8
Reputation: 4112
def fun_that_likes_multi_fun(mf:(Int, String)=>Double => Unit)={}
The function you have mentioned is a curried function. Which means the result of applying the first argument list is another function that takes the second argument list to process in entirety. So it is represented as such.
Upvotes: 10