David Herman dherman at
Wed Nov 30 09:45:54 PST 2011

On Nov 30, 2011, at 7:14 AM, Anton V. Yacenko wrote:

> It's interesting to me why () syntax returns object, and why not {}.

I assume you're talking about generator expressions, which have the syntax of array comprehensions but using parentheses as their outer delimiter. The idea originally came from Python, which uses parentheses for generator expressions.

Using {} is an interesting idea, but it could be tricky to fit that into the grammar, because of the overlap with object literals. Brendan has experimented with the idea of allowing overlap between different {} forms in other contexts:

In another discussion about do-expressions:

we talked about the possibility of allowing block statements as expressions:

    var g = { let x = f(); x * x };

and Allen Wirfs-Brock raised objections that allowing overlap between object literals and objects whose contents can be expressions will be a problem for human readers, even if formally unambiguous:

This issue will get worse as we work on extending the grammar of object literals, for example to allow computed property names in square brackets. In the context of generator expressions, this would lead to subtle differences like:

    var o = { [f(x)]: 12 };        // object literal with property name computed from f(x)
    var g = { [f(x)] for x of a }; // generator producing a sequence of singleton arrays containing f(x)

Put differently: we already overload the { } syntax for block statements and object literals, and we use the context to disambiguate. Overloading them further, especially within the same context, would lead to visual confusion.


More information about the es-discuss mailing list