Feedback sought: Optional Braces

Since this thread is active again, I’d like to mention that:

has recently been fixed by Allow indentation to work inside parens by odersky · Pull Request #10969 · lampepfl/dotty · GitHub (thanks @odersky!), both syntaxes now work.

If I’m not mistaken, that means the only remaining case where braces are currently required is the partial function literal syntax:

List(1, "a").collect({ case x: Int => x })
List(1, "a").collect { case x: Int => x }

val f: PartialFunction[Any, Int] = {
  case x: Int => 1
  case x: String => String
}

I suggest simply dropping the braces:

List(1, "a").collect(case x: Int => x) // now there's only one way to write this
val f: PartialFunction[Any, Int] =
  case x: Int => 1
  case x: String => String

This accomplishes two things:

  • we can declare victory in the War Against Braces without having to enable -Yindent-colons (or one of the alternatives discussed in this thread, which might or might not get in eventually).
  • There’s now exactly one canonical way to call a non-infix method: a.f(x), this is a big win for consistency (and IMO a reason not to proceed with -Yindent-colons or one of its alternatives, but that’s a debate that can be had post-3.0).
11 Likes