Using monocle mustache for chaining.

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Nov 12 09:48:19 PST 2011


On Nov 11, 2011, at 5:22 PM, David Herman wrote:

> On Nov 11, 2011, at 4:18 PM, Rick Waldron wrote:
> 
>> Dave, if nesting were out of the question and monocle-mustache operator always looked like an object literal as they currently exist, would it still be as vile? With that form, I'm a big fan.
> 
> I'm of multiple minds (a condition I'm gradually getting accustomed to).
> 
> 1) I'm really against the current syntax, because it hides the fact that it's a mutation. Assignments masquerading as declarative forms bring out Angry Dave (you wouldn't like me when I'm angry...)

It's not clear that it hides mutation any more than ++x or even +x (in the later case where the value of x in an object with a valueOf method that mutates the object).  In general, you have to know what syntax means to understand it.  .{ is new syntax that is defined to mean mutation.  It isn't masquerading as anything.

> 
> 2) I like it better if it includes "=" in it, e.g.:
> 
>    obj .= { foo: 1, bar: 2 };

= really isn't right either  because 
  obj.={foo:1,bar: 2};

definitely would not mean the same thing as:
obj.foo = 1;
obj.bar = 1;

The former does [[DefineOwnProperty]] and allies all the object literal static and dynamic semantics.  The later is just a sequence of [[Put]]s.

I've suggested
  obj.+{foo:1,bar: 2};
as a possible alternative if people really want a stronger lexical distinction.


> 
> 3) But that makes me really wish for a more general expression form, so I could also do e.g.:
> 
>    obj1 .= obj2;
> 
> I've even written up a strawman for it:
> 
>    http://wiki.ecmascript.org/doku.php?id=strawman:batch_assignment_operator

your strawman doesn't provide any semantics...

> 
> 4) Allen has pointed out that this is problematic for private names. For many use cases, it'd be fine if .= just didn't copy any private names -- done. But Allen's class pattern wanted to be able to specify private names. So he restricted the RHS to look like a literal.

.{ would be useless if it did not propagate private names.  Also, if it did not do super binding any all the other static and dynamic semantics of object literals.

It may not be obvious, but note that these are intended to be essentially equivalent expressions:

let obj1 = (new Object).{ /* ...* / };
let obj2 = {/* ... */};

The properties in the mustache are processed as if they were an object literal where the LHS object was the object that other wise would have been created as the first step of processing the object literal.

> 
> 5) Still, I have to say, the restricted RHS just seems ad hoc and unsatisfying.

It's essential for the only use case I care about.

Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111112/997c398e/attachment.html>


More information about the es-discuss mailing list