by zuj · 5y ago · 0 comments
(autodidact developer here, feel free to ELI5 and enlighten me)
x = some_long_expression
+ another_long_expression
problem? In Python you need to be careful to wrap that in () or use a \ before the newline to avoid it parsing as two lines, e.g. x = some_long_expression;
+ another_long_expression;
In particular, if you have a builder expressions let x = someBuilder()
.setVal(x)
.build()
You'd need to wrap the entire thing in () to avoid it getting mad, right?So if you're going to terminate expressions with ), and start them with (, why not just terminate them with ; instead?
Javascript is a "Semicolons Optional" language, but it's actually the worst of both worlds, as you have to be very careful not to wrap lines like
function() {
some stuff;
return
really_long_named_thing;
}
As it'll just insert a ; on the return, returning undefined for you.I guess that's because in the REPL Python need's to know whether the line has finished yet or not. OCaml and F# uses two semicolons ';;' for that purpose, but only in the REPL. In Haskell you can use braces and semicolons or '{:' and ':}' in the REPL.
salary = 100500
newsalary = (salary * 0.02
+ salary)
newsalary will the same as salary. There are no diagnostics.Yes, you can fix it by moving the + up a line but this should be a purely style thing, not semantics.
For example, to do method chaining across multiple lines, Python requires parentheses around the whole chain. That's a quirk of being a whitespace-dependent language.
In general, I think people who advocate for braces and semicolons just find it easier to reason about the code when they know that the formatting doesn't matter. All the flow is done via visible glyphs.
Edit: I meant to add: you need to decide whether ";" is terminates or joins statements if you have statements in the language.
The downside of assuming statements end at the end of the line is that you need to have special rules for when that isn't the case (such as Python's implicit statement continuation inside braces), while always having a terminator is simpler and more consistent (all statements end with a semicolon) at the cost of extra characters.
I'm always flabbergasted at this argument.
I'll run auto tools during check-in, but not during coding sessions.
Unless you are doing this for performance reasons, this one I'll never understand.
I'd much rather type "x.someP" and hit tab then type in "x.somePropertyThatHasARidiculousNameIAlwaysForget" after having to consult the documentation yet again to get the name.
let x = if y { a } else { b }Meanwhile Rust avoids any kind of behaviour that isn't clearly visible in the code. That includes unambiguous syntax, like the mandatory curly braces for loops and conditions. And of course the semicolon as a clear separator, which comes in handy when iterators or the builder pattern are used.
Really, Rust only doesn't look like OCaml because less people would use it if it wouldn't have braces and semicolons.
Found this post, for example: https://news.ycombinator.com/item?id=5607912
I wouldn't mind Python's whitespace indents in Rust though!
Of course you could, because a semicolon-less return doesn't work in the middle of a statement.
You can't do
bla;
a
bla bla;
b
instead of bla;
return a;
bla bla;
b
Yes, the code after the `return` isn't reachable and the Rust compiler doesn't like that ;) {123 456} + {789}
was 1245 instead of 124_245.