4 ms·
Python's solution to this is that a randomly indented block is an error. Many languages restrict statement-expressions to function calls and other expressions
by bcoates 6y ago
Python's solution to this is that a randomly indented block is an error.
Many languages restrict statement-expressions to function calls and other expressions with explicit side-effects.
Another choice would be to avoid syntax where you can optionally append expressions to an otherwise complete statement (so only one of "return" and "return expr" are allowed, and the other has an alternate spelling)
Or the parser could forbid line continuation in the absence of a trailing binary operator, which makes mid-statement newlines illegal outside of a highly visible special case so users would be trained out of inserting them at random before they introduced errors.
Both JavaScript and python do something like the last one but both screw it up; JavaScript also allows leading operators and open-brackets to continue the previous line which is a huge mess, and python uses brackets to continue lines but parens are both optional around most expressions and overloaded (generators, tuples). If you wanted to go that way you would need a bracket that meant "this is syntactically a single expression and has no other meaning"
- zozbot234 6y agoThe easiest solution might be to simply allow \ as a line-continuation operator. So you'd write: return \ x;
- colejohnson66 6y agoBut then you’ve just swapped one set of trade offs for another. It seems like a weak solution. IIRC semicolons were added to the languages of old to avoid the need for line continuation marks.