||= is much needed?

Brendan Eich brendan at mozilla.com
Sun Jun 17 19:36:05 PDT 2012


David Herman wrote:
> On Jun 15, 2012, at 5:57 PM, satyr wrote:
>
>> On Sat, Jun 16, 2012 at 4:33 AM, David Herman <dherman at mozilla.com 
>> <mailto:dherman at mozilla.com>> wrote:
>>
>>     As for null, I can see how there's confusion about whether to use
>>     null vs undefined, and so I can see why CoffeeScript would just
>>     try to blur the distinction between them.
>>
>>
>> Not just for blurring. Rejecting `null` is essential for 
>> CoffeeScript's "existence" due to `?.`, the soak/safe access operator.
>
> I think you could make a case for ?. defaulting for both but ?? 
> defaulting only undefined. The case goes something like this:
>
> - The purpose of ?? is to provide a default value when no value was 
> provided. The way to say "no value" in JavaScript is undefined.
>
> - The purpose of ?. is to fail soft when doing a property lookup. Both 
> null and undefined throw when doing a property lookup.

Agreed. This is one choice, it's plausible because of the distinction 
between defaulting (which requires intentional passing of a "please 
default" sentinel value, or not passing a trailing actual argument) and 
soaking up null-or-undefined.

Yes, we could make ?? and ??= do the same for null as for undefined. I'm 
not sure that's the right choice, but it's a choice. For foo.bar?.baz, 
though, the clearer choice is to avoid throwing, which means evaluating 
to undefined if foo.bar is missing (evaluates to undefined) *or* has a 
value not coercible to object type (null or undefined). See

http://wiki.ecmascript.org/doku.php?id=strawman:existential_operator

/be


More information about the es-discuss mailing list