What is the formal difference in Scala between braces and parentheses, and when should they be used?

后端 未结 9 1146
萌比男神i
萌比男神i 2020-11-22 07:09

What is the formal difference between passing arguments to functions in parentheses () and in braces {}?

The feeling I got from the Pro

9条回答
  •  失恋的感觉
    2020-11-22 07:54

    There is an effort from the community to standardize the usage of braces and parentheses, see Scala Style Guide (page 21): http://www.codecommit.com/scala-style-guide.pdf

    The recommended syntax for higher order methods calls is to always use braces, and to skip the dot:

    val filtered = tupleList takeWhile { case (s1, s2) => s1 == s2 }
    

    For "normal" metod calls you should use the dot and parentheses.

    val result = myInstance.foo(5, "Hello")
    

提交回复
热议问题