(Almost) everything is expression
Dmitry Soshnikov
dmitry.soshnikov at gmail.com
Fri Nov 11 04:19:23 PST 2011
On 11.11.2011 16:03, gaz Heyes wrote:
> 11/11/2011 08:07, Dmitry Soshnikov :
>
> Hi,
>
> (unfortunately Twitter is again doesn't fit for a more than
> 140 ch discussion, so moving here)
>
> I'd like to still notice the possibility of accepting the
> "almost everything is expression" approach for JS. After
> Erlang I very often miss this possibility.
>
> The idea is to make most of current complex statements as
> expressions.
>
> Dave mentioned the proposal with `do { ... }` -- yeah, it's
> fine, but much nicer is to have "all of them" as expressions.
> CoffeeScript adopted this style as well.
>
> Besides, I'd like to note, that the thing is not just
> "interesting theoretical stuff", but really is very convenient
> and useful in practice.
>
> Examples:
>
> 1. Switch-expression (as addition, I eliminated `break`)
>
> let a = switch (foo) {
> case 10: 100;
> default: 200;
> };
>
> 2. If-expression:
>
> let a = if (foo) {
> print('a is foo');
> foo;
> } else {
> // do some longer stuff
> };
>
>
>
> I think this is a bad idea,
Yeah, "right", this is why it's widely implemented and adopted by many
languages.
> the difference between a block and expression is difficult enough to
> decide (from a sandboxing perspective). It would also make missing
> semi-colons continue the expression and make it really hard for the
> developer to track down the error and cause unintended behaviour. It
> adds to the complexity of deciding if a curly is a block or literal
> and if a "/" is a divide or regexp (which is difficult enough in
> JavaScript). How on earth would this be backwards compatible too?
Yes, we have to understand which exactly form is useful for example
block-expressions. But in general I'm talking about all of those
statements, not just a simple blocks. Though of course example blocks
are used as parts of e.g. if-statements, etc.
The proposal is _very_ simple -- to allow these statements to stand on
the RHS. Regarding implementations, the statements already return
values, so it shoudn't be hard to adopt it.
>
> We of course already have for years the expression sugar for
> this -- ? : operator, but it doesn't allow to conveniently
> have longer bodies of consequent and alternative nodes of
> if-expression.
>
>
> I'm pretty sure it does: 0?0:0?0:2
I guess you see the point ;)
Dmitry.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111111/38c29b92/attachment.html>
More information about the es-discuss
mailing list