The Existential Operator

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue May 20 16:30:07 PDT 2014


then you could go like this ?

```javascript
function ifPresent(o, nmsp, dflt) {
  var r = nmsp.split('.').reduce(ifPresent.$, o);
  return r == null ? dflt : r;
}
ifPresent.$ = function (p, c) {
  return p ? p[c] : undefined;
};

var a = {
  b: {
    c: {
      d: 123
    }
  }
};

ifPresent(a, 'b.c.d', 456); // 123
```

FWIW I think having silent failures all over is not desirable so I'd like
to know what's the concrete claimed need for this, if that's possible.
(I've only read there's a need but I don't find the rationale)

Take care




On Tue, May 20, 2014 at 1:44 PM, joe <joeedh at gmail.com> wrote:

> How about an accumulator?  E.g., turn:
>
> a?.b.c.d?.func();
>
> into (if targeting ES5):
>
> var t;
> (t = a ? a.b : undefined,
>  t = t ? t.c : undefined,
>  t = t ? t.d : undefined,
>  t = t ? t.func() : undefined);
>
> (in a proper VM you'd use the stack, of course, not a temporary variable).
>
> Note how the presence of one ?. operator causes the entire chain
> (including normal .'s) to be transformed.  Not sure what the side effects
> of this would be, performance-wise.
>
> Joe
>
>
>
> On Tue, May 20, 2014 at 11:08 AM, Dmitry Soshnikov <
> dmitry.soshnikov at gmail.com> wrote:
>
>> Woah! :) 2012 -- so I correctly recalled "couple of years ago". Thanks,
>> glad it was discussed in detail.
>>
>>
>> On Tue, May 20, 2014 at 9:36 AM, Brendan Eich <brendan at mozilla.org>wrote:
>>
>>> Dmitry Soshnikov wrote:
>>>
>>> Will the "Existential Operator" for properly accessors be something
>>> interesting to consider for ES7 spec? Currently CoffeeScript uses it well.
>>>
>>>
>>> Please see
>>>
>>> http://esdiscuss.org/topic/sept-18-tc39-meeting-notes#content-2
>>>
>>> and find "ARB: This is non-compositional". Citing text for reader
>>> convenience:
>>>
>>> Existential Operator (strawman discussion)
>>>
>>> (Presented by Brendan Eich, Mozilla)
>>>
>>> Significant desire include a null and undefined check in syntax/operator
>>> form (a la coffeescipt)
>>>
>>>     o = {}
>>>     r = o?.p.q.r
>>>     r = o?.p?.q.r
>>>
>>> Mixed discussion about the needs and use cases as they apply to
>>> coffeescript code.
>>>
>>> ARB: This is non-compositional
>>>
>>>     o = {}
>>>     r = o?.p.q.r
>>>     r = (o?.p).q.r
>>>     r = o?.p.q.r()
>>>
>>> Results in…
>>>
>>>     var o, r;
>>>     o = {};
>>>     r = o != null ? o.p.q.r : void 0;
>>>     r = (o != null ? o.p : void 0).q.r;
>>>     r = o != null ? o.p.q.r() : void 0;
>>>
>>> Non-starter.
>>>
>>
>> Yeah, that's the semantics CoffeeScript uses.
>>
>>
>>
>>>  DH: Why not an operator that needs to be explicit?
>>>
>>>     o?.p?.q?.r
>>>
>>> LH: Why would you ever even use it on the first?
>>>
>>> BE: Forget all of the problems with coffeescript's impl, the need exists.
>>>
>>
>> OK.
>>
>>
>>>  YK: In the common cases, where it works, it works well. Where it
>>> doesn't, it falls apart unexpectedly.
>>>
>>> WH: What about other contexts such as p?[x], p?.q[x], and p?(x) ? [Note
>>> that grammar problems arise for some of those.]
>>>
>> The grammar can be tweaked seems.
>>
>>
>>>  General agreement.
>>>
>>> *Conclusion/Resolution* Seems useful, but not now. Semantics are unclear
>>>
>> Yeah, so now, when ES6 is closed, probably the topic can be reiterated
>> later on for ES7.
>>
>>
>>> ---
>>>
>>> The notes ended a bit too optimistically in my view! "Non-starter".
>>>
>>>
>> Yeah, for ES6 it's pretty optimistically :)
>>
>> OK, thanks again for clarifying this Brendan, and for putting the notes
>> here, appreciated.
>>
>> The only reason I put the topic here, is (not to beat a dead horse of
>> course), is that we recently had a long internal discussion thread about
>> it. I first thought, that we could just implement an internal JS extension
>> for now, and see how it goes (and if it goes well, potentially include it
>> into ES7), but, true, it needs more clarifications and thoughts.
>>
>> (still doable though)
>>
>> Dmitry
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140520/a58a7c17/attachment-0001.html>


More information about the es-discuss mailing list