News for hackers. Stuff that matters.
Re:Rust Cookbook by atbpaca on Thursday February 11, 2021 @09:09AM (#26101635)
I love Rust, I just wish it could get rid of semicolons. Quite a lot of discussions around this.
Re:Rust Cookbook by paavohtl on Thursday February 11, 2021 @09:55AM (#26102044)
As far as I know, a (C-like) language must either have:

- A statement separator (semicolons)

- Significant whitespace

Otherwise parsing the language is very likely ambiguous, e.g JavaScript [1] [2].

[1] https://flaviocopes.com/javascript-automatic-semicolon-inser...

[2] https://medium.com/better-programming/you-might-need-those-s...

Re:Rust Cookbook by jolux on Thursday February 11, 2021 @10:22AM (#26102375)
Swift and Go don't have either. Neither does Kotlin. The difference is that Rust is an expression language (much like ML), and the semicolon determines whether the resulting expression is returned or thrown away. OCaml needs semicolons when you're doing imperative programming too.
Re:Rust Cookbook by johncolanduoni on Thursday February 11, 2021 @10:47AM (#26102811)
Swift, Go, and Kotlin all take newlines into account. I guess one could have a definition of “significant whitespace” that excludes that but in any case I strongly suspect that’s what they were referring to.
Re:Rust Cookbook by jolux on Thursday February 11, 2021 @04:35PM (#26107625)
yes, but I believe they support semicolons as a statement separator on the same line
Re:Rust Cookbook by duckerude on Thursday February 11, 2021 @10:13AM (#26102262)
JavaScript messed it up, but Python did it well. A newline is a statement separator, unless it's e.g. between parentheses where a statement separator wouldn't make sense. Unlike JavaScript, that means you don't have to look at the next line to figure out whether the statement continues.

JavaScript assumes a newline doesn't end a statement, until proven otherwise. Python assumes it does end a statement unless it very clearly can't.

It's independent from Python's other significant whitespace, so you could use it in a more "C-like" syntax.

But it wouldn't be a good fit for Rust. In Rust, leaving the semicolon off the final statement in a block already changes its meaning, so that would become ambiguous. It also uses method chaining syntax a great deal more than Python, which is a bit awkward because you have to wrap the expression in parentheses (though autoformatting helps). And of course it would break most existing code.

Re:Rust Cookbook by hu3 on Thursday February 11, 2021 @10:04AM (#26102155)
Go doesn't have either: https://play.golang.org/
Re:Rust Cookbook by paavohtl on Thursday February 11, 2021 @11:11AM (#26103187)
Ah, but it does. Statements are separated by newlines. That is the same as significant whitespace (or a statement separator; you can interpret it either way).
Re:Rust Cookbook by hu3 on Thursday February 11, 2021 @11:15AM (#26103257)
Technically correct.

But the point is about ergonomy. Go doesn't require semicolons neither indentation. That coupled with gofmt auto-formatter makes coding more pleasant.

Re:Rust Cookbook by colejohnson66 on Thursday February 11, 2021 @09:22AM (#26101743)
I’d like that too, but: if you get rid of semicolons, how do you avoid something like JavaScript’s automatic semicolon insertion mess? For example:

    return
        x;
turns into:

    return;
        x;
which is clearly not what was intended. Sure, if Rust copied JavaScript’s algorithm, the compiler would throw a type error (where () is not compatible with typeof(x)) and complain of dead code instead of silently failing, but it seems like a hard task. Then again, I’m not a compiler writer.
Re:Rust Cookbook by bcoates on Thursday February 11, 2021 @10:34AM (#26102576)
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"

Re:Rust Cookbook by zozbot234 on Thursday February 11, 2021 @11:16AM (#26103271)
The easiest solution might be to simply allow \ as a line-continuation operator. So you'd write:

  return \
     x;
Re:Rust Cookbook by colejohnson66 on Thursday February 11, 2021 @12:12PM (#26104204)
But 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.
Re:Rust Cookbook by deetsb on Thursday February 11, 2021 @09:21AM (#26101741)
I wasn't a fan at first (coming from Python), but I really like that it allows you to make expressions into statements (I'm sure there's more going on then just that, but it was pretty cool to see that if/match are generally expressions but if I don't want them to be, ";" can change that!).
Re:Rust Cookbook by kthxb on Thursday February 11, 2021 @09:16AM (#26101689)
of all the complaints to have...
Re:Rust Cookbook by Tade0 on Thursday February 11, 2021 @09:17AM (#26101696)
Any such move will eventually result in automatic semicolon insertion in some shape or form.

What's wrong with semicolons anyway?

Re:Rust Cookbook by matesz on Thursday February 11, 2021 @09:20AM (#26101727)
Nothing wrong with semicolons. IMO they make code look a little bit less readable - you need to look at semicolon while you could be just looking at the empty line. One of the reasons people like python.
Re:Rust Cookbook by colejohnson66 on Thursday February 11, 2021 @09:23AM (#26101755)
This is a valid point. Without the context, the “everything is an expression” forces one to look for a line without a semicolon to see the expression result. In a 5 line function, that’s easy, but in a nested match, it can be hard.
Re:Rust Cookbook by unanswered on Thursday February 11, 2021 @12:05PM (#26104090)
It can only possibly be at the end, so it's not really hard?