Lowering curly braces in scala for multiple lines

I saw several scala code examples where several lines of code are used as a block of code without curly braces, for example:

x match { case a:Int => val b = 1 val c = b +3 println("hello!") c case _ => 5 } 

same with some very long functions that use implicit form pairs:

 a.map { implicit x => // many, many complex lines of code } 

Unlike:

 a.map { implicit x => { // many, many complex lines of code }} 

I saw a lot of documentation / frequently asked questions about the fact that several lines of code should always be surrounded by curly braces, but could not find an explanation for these exceptions. I would like to understand or have a good intuition so that I do not like magic.

+6
source share
1 answer

In the case case, the body, while it looks like a block, is actually part of the function literal expression, following the form arg => expr . Since case arguments either end with another case argument closing the curly case block, the literal function constraints are implicitly defined and the expression does not need its own block separators

+2
source

All Articles