4 ms·
There was some discussion about doing this in D, and there was some in C++11, too. I argued that the presence or absence of the ; did not visually stand out ver
by WalterBright 14y ago
There was some discussion about doing this in D, and there was some in C++11, too. I argued that the presence or absence of the ; did not visually stand out very well, and so would be a source of confusion and errors.
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 http://dlang.org/expression.html#Lambda
and in practice this has turned out to be well liked.
- pcwalton 14y agoIgnoring a potentially wanted return value with ";" is a hazard. I'd like to institute some set of "warn-unused-result" warnings in Rust to combat this.
- pdw 14y agoWouldn't the type system complain that you're returning nil in a function that is not declared/inferred as returning nil?
- dagw 14y agoWhat if foo() could return nil in some cases?
- victorma 14y agoIt can't in Rust.
- danieldk 14y agoThen you need an algebraic data type having nil is one of its constructors, and that function returning that type. AFAIK, Rust doesn't have nil. So, the closest thing is using the option type.
- kibwen 14y agoIn Rust, nil is the name for the unit type (written as empty parentheses, like "()"), used whenever a function doesn't return anything meaningful. You're correct in that instead of null pointers it has the None variant of the Option enum.
- kibwen 14y agoThis is correct for any "normal" (a.k.a. "item level") function, since their signatures must always be explicit. But you're allowed to infer the signatures of closures, which is the only place where this problem could arise. // This function lives at the top level of the module, so its types must be annotated fn foo(bar: int) -> int { // Here's a closure without type annotations: let baz = |qux| { qux + 1 }; // And a closure with type annotations: let baz = |qux: int| -> int { qux + 1 }; // Which means that this would be a compile-time error: let baz = |qux: int| -> int { qux + 1; }; // returning nil, but expected int // But if you expect this to return int, then there are very few cases where // this might not be a compile-time error: let baz = |qux| { qux + 1; }; log(error, baz(bar)); // will print "()", but did you want bar + 1? io::println(fmt!("%?", baz(bar))); // same as previous io::println(fmt!("%d", baz(bar))); // this one *is* a compile-time error // macros ftw baz(bar); // this will also be a compile-time error }
- qznc 14y agoAn understandable point of view. 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.
- ajuc 14y agoYour post made me check it, and you CAN overload "operator," in C++ apparently :) http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading#Comma_operator 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,"
- hollerith 14y agoAlthough everything you say is technically true, readers will tend to get the wrong idea unless we add that (1) most users of the do notation choose Haskell's significant white space rather than semicolons and (2) the (rare) Haskell code that does contain semicolons that behave the way you describe probably also contains semicolons (e.g., in Haskell's let statement and case statement) that have nothing to do with monads or the "sequencing" of side effects. In other words, the semicolons of Haskell are only tenuously related to semicolons in languages like C.