3

How is the following "parenthesized"?

val words = List("foo", "bar", "baz")
val phrase = "These are upper case: " + words map { _.toUpperCase } mkString ", "

Is it the same as

val words = List("foo", "bar", "baz")
val phrase = "These are upper case: " + words.map(_.toUpperCase).mkString(", ")

In other words, do implied dots (".") and parentheses have the same precedence as the real ones?

Is the first version the same as

val words = List("foo", "bar", "baz")
val phrase =
  "These are upper case: " + (words map { _.toUpperCase } mkString ", ")
Ralph
  • 31,584
  • 38
  • 145
  • 282

1 Answers1

4

Operators starting with letters have the lowest precedence. + has low precedence but higher than map or mkString. So

"These are upper case: " + words map { _.toUpperCase } mkString ", "

should be parsed as:

(("These are upper case: " + words).map{ _.toUpperCase }).mkString(", ")

Think of it as:

v1 + v2 map v3 mkString v4
((v1 + v2) map v3) mkString v4

See my other answer for more info: When to use parenthesis in Scala infix notation

Community
  • 1
  • 1
huynhjl
  • 41,520
  • 14
  • 105
  • 158