在很多Scala示例中,我看到人们在我发现完全奇怪的地方使用花括号,当使用括号可以轻松地编写相同的语句时.
例:
lst foreach (x => println(s"the value returned is: $x")) // parens
lst foreach {x => println(s"you get the idea, $x")} // braces
Run Code Online (Sandbox Code Playgroud)
我知道你可以使用大括号作为括号的替代,只是因为它允许你在多行上写一个语句:
val res = for {
x <- coll1
y <- coll2
} yield (x, y)
Run Code Online (Sandbox Code Playgroud)