||= is much needed?

Allen Wirfs-Brock allen at wirfs-brock.com
Thu Jun 14 10:33:41 PDT 2012

On Jun 14, 2012, at 10:10 AM, Brendan Eich wrote:

> On semantics: CoffeeScript's use of != null to equate null and undefined matches some users' habits, and helps to conceal the awkward fact of two bottom-types in JS (@jashkenas just averred that was the goal). But such concealment can never be perfect and other users will either want to distinguish, or (what is more worrisome) will accidentally distinguish (e.g. with !==) null from undefined.
> If only we didn't have both null and undefined! I blame Java (after myself, of course). But we're stuck with both and we have to make the best of this situation.
+1   (except Java doesn't have the equivalent of undefined as a manifest value)

> I still don't see a lot of intentional use of null to mean undefined, e.g. as a "no argument, please default" value passed into APIs. But null or any falsy value would work fine for APIs whose implementations default using ||, so it is possible null is used widely by convention, and I just haven't seen it.
> Whatever we do, we should do the same for parameter default values. CoffeeScript is consistent in equating null and undefined and triggering parameter defaulting as if by its ?= assignment op:

I think treating null as "not provided, use the default" in parameters is a bad thing that would be going down the path of concealment that is rejected above. While some (many??) people use null and undefined interchangeably, other are no doubt using them closer to the original intention (uninitialized/missing value vs.  no object).  To cause null to trigger parameter (and destructuring) default values wound break that current valid usage pattern.  

While I'm less than enthusiastic about explicitly provided undefined values triggering default value usages, I don't think it is particularly harmful.  I think treating null equivalently would be. 

> f = (x, y=1, z=2) -> console.log(x, y, z)
> f(0, null, undefined)
> generates
>  f = function(x, y, z) {
>    if (y == null) {
>      y = 1;
>    }
>    if (z == null) {
>      z = 2;
>    }
>    return console.log(x, y, z);
>  };
>  f(0, null, void 0);
> We could certainly do worse than to pave this cowpath.

sure, for example, by having any falsy value trigger default value usage.  But just because there are worse things doesn't make the a good idea.


More information about the es-discuss mailing list