3 ms·
Many languages are expressions (as opposed to statements) including C-like languages. I've always thought that 'return' itself was redundant. If a compound stat
by JoeAltmaier 12y ago
Many languages are expressions (as opposed to statements) including C-like languages. I've always thought that 'return' itself was redundant. If a compound statement was defined to return the value of the final (executed) statement, then functional methods would get much simpler. And remove the need for the '?' operator for instance.
e.g.
int foo(int x) { bar(x,y); }
or
x = {if (foo() > 9) true; else false; }
- eru 12y agoYou might like functional languages.
- thedufer 12y agoHave you tried coffeescript? Your examples are pretty close to valid code that does what you want.
- _lce0 12y agoI think one problem with statements-as-expression is that is not always evident which should be their result. for instance: bool r = if (test()) { false; } else { true; } what r should be set to? true or false? I don't see the point of adding such complexity.. it's waaay more clear to be explicit bool r = test(); if (r) { false; } else { true; } or, if you intended the other way bool r; if (test()) { r = false; } else { r = true; } I guess that's why you need to be strictly correct, or highly opinionated, to design a language.
- JoeAltmaier 12y agoI'm missing your point I guess. All those are equivalent? In C an assignment has the value of the RHS, so that doesn't change anything in these examples.
- molo 12y agoThat is unnecessary verbosity. Try this instead: bool r = !test();
- eru 12y agoIn an imperative language, you still might want an optional return to jump out of the middle of some code.