Kotlin forces open brace style in some situations

I realize that JetBrains subscribes to the hidden open brace style of putting their open braces at the end of the line which s fine for your code. You are free to be WRONG :wink:

I am firmly in the camp that open braces belong on their own line (except in some cases where the entire block is small and fits on one line) and I don’t like being forced to do it differently. I have found one situation where Kotlin is trying to force end of line style for opening braces.

The case is when you have a member or extension function that only takes a function argument and you are trying to drop the parentheses. A good example would be functions like apply or map.

I should be able to write code like this:

foo.apply
{
    bar = 5
}

But that does not compile unless I move the opening brace to the previous line.

Back when any function could be used as an infix function that was permissible as:

foo apply
{
    bar = 5
}

But now that apply cannot be used as infix it isn’t allowed.

If I wrap the lambda with parentheses it does compile:

foo.apply(
{
    bar = 5
})

or even when adding empty parentheses like this:

foo.apply()
{
    bar = 5
}
2 Likes