statements that could be expressions?

Waldemar Horwat waldemar at google.com
Wed Jun 1 18:30:48 PDT 2011


On 06/01/11 18:01, Peter Michaux wrote:
> On Wed, Jun 1, 2011 at 5:52 PM, Waldemar Horwat<waldemar at google.com>  wrote:
>> On 06/01/11 16:07, Peter Michaux wrote:
>>>
>>> Could some of JavaScript's statements also be allowed as expressions?
>
> [snip]
>
>> Theoretically it's possible to allow statements as expressions, but you run
>> into a number of practical problems:
>>
>> - Precedence inversion:
>>
>> a = if (b) c
>>
>> So far so good.  However, the substatement of an if can also be a comma
>> expression, which causes trouble:
>>
>> a = if (b) c, d
>>
>> Also, what should be stored in a if b is false?  You can argue between
>> false, null, and undefined.
>
> Indeed.
>
>
>> - Conflict between blocks and statements:
>>
>> a = {x: while (x) { ... break x;}}
>>
>> is either an object initializer or a block containing a labeled while
>> statement.
>
> Ouch.
>
>> - Semicolon insertion:
>>
>> return
>>   if (longcondition) {
>>     ...
>>   } else {
>>     ...
>>   }
>>
>> will not do what you want.  The entire if is dead code.
>
> This one is already an issue for JavaScript programmers.
>
> --
>
> Ok so perhaps only some statements could be converted to expressions
> easily without ambiguity. Would a throw expression be possible without
> much grief, for example? I think it could be useful (since I wanted to
> do it exactly two days ago hence my post here.)

You'd have to deal with the same precedence inversion as in the if statement example above.

If you make <throw expr> bind the loosest, then there is no inversion, but you can't write things like:

a = b ? c : throw d;

because you made ?: bind tighter than throw.

If you make <throw expr> bind tighter than, say, ?:, then you run into an ambiguity with = or , operators:

a = b ? c : throw d, e;

This can parse as either:

a = b ? c : throw (d, e);

or:

(a = b ? c : throw d), e;

     Waldemar


More information about the es-discuss mailing list