5 ms·
> 2. We could make "if" an expression. We could make everything an expression for that matter, though there's some pros and cons there. I'm not sure if this is
by JadeNB 12y ago
> 2. We could make "if" an expression. We could make everything an expression for that matter, though there's some pros and cons there.
I'm not sure if this is the place for such a far-reaching discussion, but I've never understood this point. What are the cons to making everything an expression?
- tptacek 12y agoHere's a bunch of Pythonistas grappling with this question: https://mail.python.org/pipermail/python-ideas/2008-September/001932.html https://mail.python.org/pipermail/python-ideas/2008-Septembe...
- munificent 12y agoUntil recently I was firmly in the "statements are evil" camp. Now my perspective is a bit more nuanced. There are two facets to "everything is an expression": 1. Any grammatical production can be used in an expression. 2. Function/method bodies implicitly return the result of the last expression they contain. I really like #1, though even expression-oriented languages limit this. There's still something akin to "operator precedence" at the "statement-list" expression level. If, for example, you look at Ruby's grammar, you'll see it has productions called "statements". This is to rule out totally bizarre things like: 1 + while (foo) { ... } for (for (for (...) {}) {}) So, even if you treat everything as an expression, there are still some limitations as to where different grammatical productions can appear. In practice, I think the main thing you gain (in a language with an imperative-style syntax like Wren, Ruby, etc.) is the ability to have the right side of a return or assignment be a branching statement. Stuff like: var fizzBuzz = if (n % 15) { "FizzBuzz" } else if (n % 5) { "Buzz" } else if (n % 3) { "Fizz" } else n The other statements are less useful. I'm not even sure what the result of a looping expression should be, but I'm certain that I don't like CoffeeScript's answer (an array of all of the elements, which often gets discarded!). Break and return statements have no useful results. Variable declaration statements make it really hard to reason about scope when used in the middle of an expression. All this means that I think loosening up the grammar to make every statement an expression is mostly only useful to make branching an expression. The other half is having functions implicitly return the result of the last expression. I really like that in some functions for brevity, and really dislike it in others. This may not be rational, but creeps me out that lots of Ruby methods just leak random values. For me, I'd rather have an explicit form for bodies that implicity return their result and have the default be to not do that. So that's what I did for Wren. A single-line body implicitly returns its result, but a multi-line one does not: class Person { name { "Fred" } // Implicit return. sayHi { IO.write("Hi, I'm Fred!") // Returns null. } } I like this more than always having an implicit return, so this addresses point #2 above. I'm not opposed to allowing branch statements to be expressions, but I worry that the confusion it causes may outweight the benefit, so I conservatively left it out for now. Having a statement/expression distinction also makes the bytecode compilation a bit simpler. It's easier to tell which things emit values and which don't, but that's not hard to fix.
- tonyg 12y ago> I'm not even sure what the result of a looping expression should be This is one of the reasons I like having proper tail calls be part of a language specification, incidentally :-) Reason being that loops are then simple derived constructs, and their meaning (as expressions and otherwise) follows from their definition. In the specific case of "while", the value of a while-loop-like form would usually be null/void, because of the one-armed-if it expands into. Regarding implicit return values: in your example, the sayHi method would naturally return null if IO.write returned null, even with implicit return values. Having a strong library convention that imperative procedures return null seems to prevent programmers from trying to rely on the results of imperative procedures, even where implicit return values are part of the language. My support for this claim is only anecdotal; it comes from working with programs written in Racket, which has both implicit return values and a strong convention of returning void from imperative procedures.
- munificent 12y ago> In the specific case of "while", the value of a while-loop-like form would usually be null/void, because of the one-armed-if it expands into. Right, which doesn't make it very useful as an expression. > Having a strong library convention that imperative procedures return null seems to prevent programmers from trying to rely on the results of imperative procedures I have the opposite feeling (possibly because Wren doesn't have implicit returns). Since library calls are expressions, they may as well return something useful when possible. For example IO.print() returns its argument. That's handy for debugging the result of an intermediate expression. Say you've got: someMethod(anotherOne(arg)) And something weird is going on. Giving IO.print() a return value lets you inject it in the middle: someMethod(IO.print(anotherOne(arg))) without having to hoist the subexpression out. > My support for this claim is only anecdotal; it comes from working with programs written in Racket, which has both implicit return values and a strong convention of returning void from imperative procedures. Yeah, that makes sense. I think the important idea is to be consistent.
- jpfed 12y ago> I'm not even sure what the result of a looping expression should be >Break and return statements have no useful results The result of a looping expression could be specified by its break! var myNeedle = foreach(var maybeNeedle in myHaystack) { if (maybeNeedle.isTheNeedle()) break maybeNeedle; } if (myNeedle != null) print("I found " + myNeedle.toString());