How to understand such a function declaration: `=> .. => .. => ..`?

I see this scala function declaration somewhere:

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

I have never seen such a function: => ... => ... => ... how to understand it?

+4
source share
2 answers

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

=> A is a by-name parameter of type A Therefore, => String => Result[AnyContent] => Result indicates that test accepts an argument of type String => Result[AnyContent] => Result by name. More about by-name options ... here .

+10
source

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

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

Same as

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

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

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

+1
source

Source: https://habr.com/ru/post/1399627/


All Articles