There’s a “gotcha” either way
A lot of these are cases where the code could do either what you expect or something else. These ‘gotchas’ mean that no matter what the behavior the ambiguity could cause confusion.
To avoid confusion we can:
- code in the standard style to bring clarity
- code with some explicitly-ness added in to bring clarity
#2 is interesting because it keeps consistency. For example, this style forces consumption of the lambda and throws an error if it’s too many args:
bar()
{} // Always treated as an arg
The downside with trying supporting consistency for both styles (what OP is asking for) that is that it sacrifices consistancy when combined with some language choices. Trailing lambdas and implicit semicolons are features that run counter to next-line styling.
I can see an argument for limited changes. I’m mostly speculating on there being more corner cases that would be annoying to solve. You’d probably still need a version bump in Kotlin to change the grammar right?
{ A bit of a word spill here. It’s related but enough of a side topic that I’m hoping to keep it from being a distraction
}
Compiler choices and style rant
This is one of several places code would get ambiguous.
The compiler’s interpretation switching to match interpretation, and favor including the next-line, is equally reasonable compared the one statement interpretation. No matter what there is a “gotcha”. If we must pick an interpretation to favor, shouldn’t the official style be favored?
Sacrafice consistency might be okay?
With that said I think you might have a stronger argument on very specific cases. Let’s look at the parathesis requirement for trailing lambdas on the next line.
In favor of forcing parentheses:
The compiler is helping you write consistent code by forcing you to explicitly choose the less-kotlin-like interpretation (the less common one) and won’t compile any other way when you write parathesis-\n-lambda.
The compiler errors on
println("")
{} // Error, this is consistently seen as an argument even though both lines could be valid statements
One could argue that it’s pragmatic to handle this case conditionally. Like when one statement is invalid. This is what I and gidds were originally suggesting just on different criteria. One could argue it’s worth limited sacrifice of the consistency–limiting to very specific cases might make the argument stronger.
Style isn’t always subjective
I want to make the case that it’s okay for a language’s official style to be favored, and for languages to not support a wide range of styles.
For example, Kotlin does not support next-line style to the same degree as Java. When the compiler is given equally valid options for interpreting statements. The compiler favors the interpretation that lines up with the official style.
For many languages, syntax features will be implemented favor a style. Sometimes language features enable more flexibility (i.e. Java ignoring all whitespace). Other languages will not allow that at all flexibility (which is just syntax at that point) such as Python using whitespace scope.
Kotlin is somewhere in the middle–it is flexible but requires slight code edits for style changes. Features like trailing lambdas and implicit semicolons force style to tie into language features. Kotlin supporting fewer styles enables more freedom in implementing these features.
A style being supported could be thought of as a language feature. It puts limits on future features. Seeking to support an additional style, such as ignoring all whitespace, comes at a huge cost.
Always prefer the standard, even in practice
I like the saying “we code for humans, not for computers” because it highlights that the top priority of source code is clear communication with humans. In this context, I’d add that “we code for other humans, not for ourselves”. Code is about communication–it’s the reason we can say one should always prefer your team’s style. In the same vain, your team should prefer the community’s style over its own.
I’d suggest that even in your own private code, which will never be seen by another human, you should still code in the official style. The reason being is that if one wishes to communicate clearly in any language/dialect, they should try to communicate as the native speakers do. They should practice and become comfortable with the idioms, grammar, structure of the words as it is spoken by the community.
Yes It’ll be uncomfortable at first but personal taste is malleable. Even if there isn’t enough time to un-due one’s discomfort in the style, that’s okay. At least they communicated clearly.
Don’t lead your team into your dialect
Choosing your teams style is good for communication, but what if your preferred style is chosen?
I have seen some people get away with making their team choose their style. This person liked to space their code differently with spacing and alignment. They also valided consistancy and the team adapted their dialect. They got their preferred style and consistency!
IMHO this is putting one’s preference over your teams needs to communicate in the larger community and their project’s/team’s future. Team members change. Code doesn’t live in isolation. Making a team adapt a preference that’s against the grain is not good for the team.
Teams sometimes respond with “it’s just preference, we can appease your style needs” to keep the top developer happy–but that’s an unfortunate scenario. I’d hope all of the developers, especially the most valuable ones would seek the best for their project and team even if it means some discomfort in style.