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.