Hi, my proposal is to allow braces-less `try`/`cat...
# language-proposals
l
Hi, my proposal is to allow braces-less `try`/`catch`/`finally` as done for `if`/`else`. Here's an example of how it could look like:
Copy code
try registerAppInstance(info = AppInstanceInfo(
    someToken = someValue
)) catch (e: HttpException) ui.showError(
    when (val code = e.code()) {
        503 -> ServiceUnavailableTryLater
        ...
        else -> ...
    }
)
👍 1
d
Why stop there -- make the "when" clause braceless too
😕 1
g
looks messy on this example:
Copy code
try {
    registerAppInstance(info = AppInstanceInfo(
        someToken = someValue
    ))
} catch (e: HttpException) {
    ui.showError(
        when (val code = e.code()) {
            503 -> ServiceUnavailableTryLater
                ...
            else -> ...
        }
    )
}
Reads much better
But I agree, maybe in some cases it would work better, but such code would not probably pass code review, the same way as complex if/else without braces
l
Well, not everyone has his code reviewed 😅
g
yes, I agree, but would be good to see some real use cases when it looks better without braces to support this feature
l
All the cases where the single expression in one of the blocks can fit on the line?
g
Yeah, something like this
interesting actually how it would work, because
if
and
while
are different, they have expression in round brackets
operator(expression)
so parsing is different from
try
l
`if`/`else` is very similar.
when
on the other hand is different because the optional braces are for the branches only.
g
sorry, I mean
while
, just mistake, fixed my message
if/else and while are similar, both support braces-less syntax