172 points · by joeyespo · 14y ago · 74 comments
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.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.
Though I can think of an even more crazy variation. Declare the semicolon a binary operator and allow overloading it.
As funny as that sounds, Haskell provides something like this, as do-notation can behave differently depending on the monad it is in.
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/O...
My mind is blown. You can write bottom-up code in C++ :)
EDIT: no, you still can't the expressions are evaluated first, before calling "operator,"
In other words, the semicolons of Haskell are only tenuously related to semicolons in languages like C.
Almost 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.
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.The semicolon is the least-annoying non-letter character to type. It's right there on your home row.
Still, even on an American keyboard () are not terribly well placed.
I've been thinking about adopting the NEO2 layout(with some modification so that I get all of åäö) but haven't gotten around to it yet. Has anyone tried that for programming?
Russia uses shift+4. Can't imagine how annoying that is.
We're trying to mix logic that executes on each element in a sequence, with logic that controls how to iterate over that sequence. That is, a "return 42" statement will tell .each to stop iterating, but its contained within a block that is supposed to do things to individual elements.
The mathematical concept just doesn't sit right with me.. I guess if all you have as an iterator is 'each' then that would necessitate finding an additional way to modify iteration in some way, but still.. I don't like the fact that a return statement can break out of something outside of its own scope
edit: even the low-level alternative seems nicer
for(blah;blah;blah) {
stuff;
}It also means you can't put more expressions on the same line because doing so requires a semi-colon which then eliminates the special semicolon behavior. Having an explicit 'ret' keyword means you could accomplish that and have more expressions for a separate block on the same line if desired.
1: http://c2.com/cgi/wiki?SyntacticallySignificantWhitespaceCon...
a ; b is the operator which returns the value of b. And, there is some syntactic sugar to make a ; equivalent to a ; nil which returns nil.
However, that template code was utterly hideous.
fn find_even<T: Num>(vec: &[T]) -> Option<T> {Only sorta - consider Haskell's indent rule: code which is part of an expression should be indented further than the start of that expression. Generally, this is something you should be doing to make your code readable regardless of the statement termination.
The presence/absence of the ; is both explicit and subtle enough to not annoy.
map(lambda t: t ** 2, [1,2,3,4])
or even: [(lambda t: t**2)(x) for x in [1,2,3,4]]Why would you have that lambda?
[x**2 for x in range(1, 5)](As a prerequisite to making its point, the article teaches many intricate details regarding two dynamic languages which even most of their practitioners probably never think about, then dives into more intricate details regarding a language which most of us have probably never even used, let along grokked all the details of. I sense that there's something important here for me to learn, but from where I sit this article is a lot to bite off all at once.)
A tight summary by someone who understands all this would be appreciated by many readers, not just myself.
A tight summary of all of it would be as long as the article. However, the point about semicolons in Rust is:
1. ; is a separator, not a terminator.
2. a;b separates a and b.
3. a; is a special case which means a;nil(or whatever is the equivalent in Rust)
4. The last expression in a function will be the return value of the function.
5. If the last line in a function is "a", it returns a. If it's "a;", it returns nil(from 3)
Why all this talk about Python and Ruby? Rust is not really competing with those languages-- it is quite specifically designed as a C++ replacement. They should be comparing themselves against C++, Golang, or D.
The Golang solution would just be a function which returns the next element or nil if there are no more elements. That function might be part of an interface, if you wanted to generalize it across several types. To me, this is a lot simpler than the other stuff that was discussed.
To go back to Rust specifically, rather than having magic semicolons, why not make the "break" statement take a value, which becomes the return value of the block? Despite all the tl;dr there was not much discussion of design alternatives.