Omitting brackets

Here is the Scala code

#1 def method1 = { map1.foreach({ case(key, value) => { println("key " + key + " value " + value) } }) } #2 def method1 = { map1.foreach{ case(key, value) => { println("key " + key + " value " + value) } } } 

These are almost numbers for me, but, nevertheless, I want to clarify: why in this case can the brackets be omitted?

+4
source share
1 answer

You can always copy the argument arguments of the arguments to the braces in Scala. for instance

 def test(i: Int) {} test { 3 } 

The basis for this is the definition of the argument expressions covered by §6.6 Scala Language Specification (SLS) :

 ArgumentExprs ::= '(' [Exprs] ')' | '(' [Exprs ','] PostfixExpr ':' '_' '*' ')' | [nl] BlockExpr 

Curly braces are covered by the last case (block expression), which is essentially '{' Block '}' (see the beginning of Chapter 6 of SLS).

This does not apply to conditional expressions, if , (§6.16 SLS) and while (§6.17 SLS), but it works for for concepts (§6.19 SLS), somewhat inconsistent.

A statement of conformity to a pattern or conformity to a pattern with anonymous literal functions on the other hand needs to be defined with curly braces, for example. { case i: Int => i + i } , parentheses are not allowed here (§8.5 SLS).

In a method call, foreach accepts an argument to the function, so you can discard extra parentheses or double brackets:

 List(1, 2).foreach({ case i => println(i) }) List(1, 2).foreach {{ case i => println(i) }} List(1, 2).foreach { case i => println(i) } // no reason to have double braces 

In this case, pattern matching really doesn't buy you anything, and you can use a regular (asymmetric) function, and therefore the following will work:

 List(1, 2).foreach(i => println(i)) // §6.23 SLS - anonymous functions List(1, 2).foreach(println) // §6.26.2 / §6.26.5 - eta expansion 

In your case, however, the Map Map method passes a tuple of key and value to a function, so you use case matching to deconstruct this tuple, so you must have curly braces. This is better than writing

 map1.foreach(tup => println("key " + tup._1 + " value " + tup._2) 

As a side note, applying braces around patterns matching the body of the case is considered a bad style; they are not needed, even if the body covers several lines. Therefore, instead of

 case(key, value) => { println("key " + key + " value " + value) } 

you should write

 case (key, value) => println("key " + key + " value " + value) 

This blog post contains several polemics regarding the various uses of curly brackets, dots, and parentheses in Scala (the "What I don't like" section). In the end, you have to decide which style is better - this is where people who advocate “stubborn” versus “inexorable” languages ​​fight each other.

In general, you need curly braces when an expression spans multiple lines or if you have a pattern match. When calling methods with multiple parameter lists, often the last list contains a single function argument, so you get a beautiful looking subjective proposition about the course syntax:

 val l = List(1, 2, 3) l.foldLeft(0) { (sum, i) => sum + i } 
+4
source

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


All Articles