Existential Operator / Null Propagation Operator

Herby Vojčík herby at mailbox.sk
Tue Apr 7 18:54:31 UTC 2015



Kevin Smith wrote:
> We should perhaps review this old thread:
>
> https://esdiscuss.org/topic/fail-fast-object-destructuring-don-t-add-more-slop-to-sloppy-mode
>
> for another possible way to avoid non-compositionality.  (Look for the
> suggestion about "Nil".  It's basically an exotic falsey object which
> returns itself for any property lookups or calls.)

Going a bit deeper this way, this thing (changing ref to Nil which 
always returns Nil upon call, construct and get, and getting back 
undefined when value is needed) have nicely separated concerns:

Let's say '?foo', as an unary oper
ator, when foo is a reference, returns Nil when foo is reference to null 
or undefined, otherwise leaves it unchanged. Then:

  ?a.b.c just works,
  ?d() just works,
  new ?e[i]() just works, etc,. since ref is changed to Nil if the thing 
is null/undefined, it propagates through calls and gets, then changes 
itself to undefined value. The priority of ? must be pretty high, 
though, to only apply to nearest token.

Plus, it can be used for "normalizing" null/undefined to undefined:

  var normalizedFoo = ?foo;

Seems sort of nice that it is separated and there are no special 
operations for ?., ?(, ?[.

Herby


More information about the es-discuss mailing list