function-literal

Multiline function literal as arguments in Scala

℡╲_俬逩灬. 提交于 2019-11-27 04:38:50
I always wondered why sometimes with function literals we can ignore the curly brace even for multiple statements. To illustrate this, the syntax for a multiline function literal is to enclose the statements with curly braces. Like so, val fl = (x: Int) => { println("Add 25 to "+x) x + 25 } However, when you pass it to a single-argument function, you can ignore the required curly brace for the function literal. So for a given function f, def f( fl: Int => Int ) { println("Result is "+ fl(5)) } You can call f() like this, f( x=> { println("Add 25 to "+x) x + 25 }) ------------------------- Add