Such a Little Thing: The Semicolon in Rust
lucumr.pocoo.org
Such a Little Thing: The Semicolon in Rust
1–10 of 81 posts
Re: Such a Little Thing: The Semicolon in Rust
#2Almost a year later, I'm just as in love with Rust's semicolon rules as Armin is. However, I bet new users will still be just as instinctively revolted as I initially was.
Re: Such a Little Thing: The Semicolon in Rust
#3Re: Such a Little Thing: The Semicolon in Rust
#4Re: Such a Little Thing: The Semicolon in Rust
#5 foo() => evaluates to the return value of foo
{ foo() } => evaluates to the return value of foo
{ foo(); } => evaluates to nil
The last case is not a special case. The last expression in the block determines the returned value. There is an expression separator in there, so there must be two expressions which are separated by it. The first one is foo() and second one is ... wait for it ... the empty expression. Which value should EmptyExpression have? Of course: nil, which would be called void in C-land.Re: Such a Little Thing: The Semicolon in Rust
#6The semicolon is the least-annoying non-letter character to type. It's right there on your home row.
Re: Such a Little Thing: The Semicolon in Rust
#7Is that really some "special behavior"? As far as i understand the semicolon is just an expression separator like in Erlang and Pascal. In contrast, the semicolon is a statement terminator in C. C uses the comma for expression separation. Practically everything is an expression in Rust, even blocks, so foo() => evaluates to the return value of foo { foo() } => evaluates to the return value of foo { foo(); } => evalua…
Hence, ; remained as a statement terminator, and the return keyword served to indicate returning an expression.
However, the D lambda syntax does not require a ; when the lambda body consists of only a single expression:
http://dlang.org/expression.html#Lambda
and in practice this has turned out to be well liked.
Re: Such a Little Thing: The Semicolon in Rust
#8Is that really some "special behavior"? As far as i understand the semicolon is just an expression separator like in Erlang and Pascal. In contrast, the semicolon is a statement terminator in C. C uses the comma for expression separation. Practically everything is an expression in Rust, even blocks, so foo() => evaluates to the return value of foo { foo() } => evaluates to the return value of foo { foo(); } => evalua…
Re: Such a Little Thing: The Semicolon in Rust
#9 The downside is that you would have to put () (Rust's version of “nil”) in a bunch of functions to fulfil the requirements of the callback's signature since otherwise the type inferred from the function would be the value of the last expression
Wouldn't co/contra-variance solve this entirely? It works just fine in Scala for example scala> def runTwice(f: () => Unit) = {
| f()
| f()
| }
runTwice: (f: () => Unit)Unit
scala> runTwice{ () =>
| println("moo")
| 1
| }
moo
moo
Note how it expects a function that returns Unit, i'm passing in a function that returns an Int (1), but the compiler is perfectly happy.Re: Such a Little Thing: The Semicolon in Rust
#10Are semicolons annoying to type? Probably, I got used to them The semicolon is the least-annoying non-letter character to type. It's right there on your home row.