I have a question about language design: why does ...
# announcements
l
I have a question about language design: why does kotlin use curly braces ? jetbrains has shown us that they put great thought into what actually matters and is logically the best choice even when it clearly goes against what people are used to and would instinctively support (most prominent example of that would be "public by default" which is disliked by almost anyone who's new to kotlin). The fact that we indent all our code even though the language doesnt require it shows that we (the humans) read code via indent and not via braces. so why not remove them altogether and make scoping indentation based (like python or haskell) to reduce verbosity? I know that many people instinctively dislike this idea, and I have ask as many people that prefer braces as possible trying to understand their reasoning, but I have yet to find a single valid point that speaks for curly braces over indentation based syntax. i seems to simply come from habit. to me curly braces are like semicolons, they used to be absolutely mandatory for the compiler to work but with modern technology have since become obsolete and are only kept around for legacy reasons. but maybe i am missing something, maybe there is a case where braces provide some sort of advantage that havent thought of yet? to be clear i dont suggest kotlin to change in that regard, thats clearly impossible at this point even if we wanted to. I'm just really interested in language design and how and and why certain decisions are made the way they are.
💡 3
c
A large design goal of Kotlin was to make everything explicit. Don’t rely on an operator when a named function will do, etc. Using indentation for scoping is definitely more on the “implicit” side of scoping, while curly braces make scopes explicit. Even as far as lambdas need to be enclosed in curly braces, unlike a Java lambda, for example. You can put a lambda on a single line, but you still have to acknowledge that the lambda is a child scope with braces.
👍 6
1
m
And from a pragmatic perspective, I suspect it would make it harder to convince Java people to move, as it's quite the mindset shift to move to indentation based scoping.
👍 1
l
considering how many "mindset shifts" kotlin already has, i doubt that it would make any difference at this point
s
I think it's something that, unlike "public by default" is more of just a cosmetic thing. As such, it's best to make it something that more people are going to feel comfortable with
c
@LastExceed what would you consider to be the major mindshits in adopting Kotlin? I’ve always felt like Kotlin was very good about not requiring major mindshits, and allowing it’s bigger changes to be adopted at your own pace (like coroutines being a separate library)
1
👍 2
k
I know this is opening a can of worms but moving code around in Python is always a fight against indentation-based scopes: <https://kotlinlang.slack.com/files/U5UU34LPK/FRC9C6EQN/2019-12-04_20-32-30.gif>
👍 1
(as a response to your "but I have yet to find a single valid point that speaks for curly braces over indentation based syntax")
c
I find the same kinds of problems when using YAML. Things almost never get pasted at the indentation level I want
l
@Casey Brooks companion objects instead of statics, public by default, putting types behind (instead of before) the name, to name a few
@karelpeeters i dont quite understand the problem you're having (idk whats going on in that gif), can you elaborate?
k
That's duplicate line, move line up and reformat in sequence. I basically want to copy/paste the print to before the if and then reformat the code.
l
well that sounds like an editor problem rather than a language problem
k
It happens in every editor, and this problem literally cannot happen in
{}
-based languages no matter how dumb the editor or auto-formatter is.
👍 1
💯 10
l
hmm i'm afraid i still haven't quite understood the problem, because to me it looks like everything works fine in the gif you posted
k
The
if
should have stayed in the loop body.
l
ok but how would the editor know?
ah i get it, thats precisely the point you're trying to make
💡 3
👌 7
k
If you try the same thing in Kotlin it just works™.
l
alright then. its quite an edge case since you could just use tab or shift+tab to be explicit about what you want, but it is indeed a problem that you wouldn't have with braces, so it counts
k
Yeah you can easily fix it, but now you're constantly spending effort manually indenting. Also this is just one example, it also happens around copy/paste and probably other things.
l
yeah i can see how that would become really painful when copy pasting, definitely a point for the braces
👍 1
m
one nice thing with curly braces is exactly what Karel is bringing up. I paste inside the curly.braces I care about and have ide reformat code. So I don't have to spend time reviewing indentation levels or manual format. I've never liked indentation based scoping. Takes more cognitive effort to ensure I haven't screwed it up and more manual effort to format it.
👍 2
☝️ 5