Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand this kind of function declaration: `=> .. => .. => ..`?

Tags:

scala

I see this scala function declaration in somewhere:

def test(f: => String => Result[AnyContent] => Result) = ...

I never saw this kind of function: => ... => ... => ..., how to understand it?

like image 318
Freewind Avatar asked Dec 22 '22 01:12

Freewind


2 Answers

String => Result[AnyContent] => Result desugars to Function1[String, Function1[Result[AnyContent], Result]]. It's helpful to read it as: => String => (Result[AnyContent] => Result]). That is, a function that takes a => String returns a function Result[AnyContent] => Result (also known as curried function).

=> A is a by-name parameter of type A. So => String => Result[AnyContent] => Result indicates that test takes an argument of type String => Result[AnyContent] => Result by-name. Learn more about by-name parameters here.

like image 183
missingfaktor Avatar answered Jan 04 '23 07:01

missingfaktor


Remember that a function is a normal data type. Functions can return functions.

f: => String => Result[AnyContent] => Result

Is the same as

String => ( Result[AnyContent] => Result )

This is just a function from String returning a function from Result[AnyContent] to Result.

f: => is a by name parameter as explained by Josh in the answer above.

like image 36
Jan Avatar answered Jan 04 '23 08:01

Jan