Confusing Scala Higher Order Function Call Syntax

The various uses of "block" {...} contruct in scala confuse me a bit, especially when calling a higher-order function, as in the following example.

def higherOrder(func: Int => Int): Int = { func(4) } val f = ((x: Int) => x*x) 

Then I can call a higher password like this:

  • higherOrder(f) , or

  • higherOrder {f} , or

  • higherOrder { x => x*x }

(1) is obvious, but I can't turn my head around the way the syntax for (2) and (3) is parsed by the compiler Can anyone explain what (2) and (3) correspond to in terms of language specification?

+4
source share
1 answer

See SLS 6.6 Function Applications . The purpose of the function is determined as follows:

 SimpleExpr ::= SimpleExpr1 ArgumentExprs ArgumentExprs ::= '(' [Exprs] ')' ... | [nl] BlockExpr 

And BlockExpr is

 BlockExpr ::= '{' CaseClauses '}' | '{' Block '}' 

So, after the name of the function or method, you can specify a list of arguments in brackets or an expression in curly brackets.

+4
source

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


All Articles