Eliminating braces for one-liner blocks with `fewerBraces` enabled

I’m using indentation-based syntax for a while, but just now started experimenting with the experimental fewerBraces feature turned on.
I found interesting the possibility of getting rid of braces for one-liner blocks. See the following example:

class MyIf(cond : Boolean):
  def myelseif(cond : Boolean)(block : => Unit): MyIf = ???
  def myelse(block : => Unit) : Unit = {}

def myif(cond : Boolean): MyIf = ???

With fewerBraces on, it’s possible to write:

myif(cond1):
  println("first branch")
.myelseif(cond2):
  println("second branch")
.myelse:
  println("else branch")

It is very compelling to write it like as follows, but currently impossible:

myif(cond1): println("first branch")
.myelseif(cond2): println("second branch")
.myelse: println("else branch")

The possibility for one-liner code blocks is only to use braces:

myif(cond1) {println("first branch")}
.myelseif(cond2) {println("second branch")}
.myelse {println("else branch")}

Can a colon without a new-line and indent be a replacement for a single-line block?
If so, what will be the rule in the following case?

myif(cond1): 
.myelseif(cond2): println("second branch")
.myelse: println("else branch")

Is it an error or equivalent to the following code?

myif(cond1) {}
.myelseif(cond2): println("second branch")
.myelse: println("else branch")

@odersky, I would love you view on this.

2 Likes

I thought about this a bit before. The parsing problems for allowing this look really hairy. And that’s never a good thing to have. Even if we could overcome it by some ingenuity it would make things brittle.

I actually found your original indented example by far the clearest and most pleasing to read. So I’d say let’s keep it at that and be opinionated about layout.

1 Like

Ok, understood.

1 Like