||= is much needed?
Brendan Eich
brendan at mozilla.com
Thu Jun 14 10:10:19 PDT 2012
Confusion: at no point has the
http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
strawman removed the infix operator originally proposed as ?? and
currently spelled ?: -- everyone agrees on having both OP and OP=,
whatever the spelling of OP.
I agree on reflection with Wes and others who've objected that A ?: B
has the simplest interpretation as A ? A : B and therefore should not be
used for anything like (A !== undefined) ? A : B or (A != null) ? A : B.
I noted this as an open issue but I'm almost ready to flip the strawman
back to ?? and ??=. Comments on syntax?
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.
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:
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.
/be
David Herman wrote:
> On Jun 12, 2012, at 10:52 PM, Brendan Eich wrote:
>
>> People don't default on the caller side (at the callsite) much, in my experience. Dave may be seeing other sources, but it's extremely rare in my experience to see
>>
>>
>> foo(arg1 || callers_idea_of_default_arg1_value);
>
> I'm sure it's more rare than the assignment form, but that's partly because the assignment form is needed to make up for lack of pdv's.
>
>
>> whereas we all see
>>
>> function foo(a, b, c) {
>> a = a || default_a;
>> b.x = b.x || default_b_x;
>> b.y = b.y || default_b_y;
>> c.z = function (w) {
>> // long body here
>> }
>> ...
>> }
>
> Well, you wouldn't argue against having || in the language. Yes, I know it serves other roles as well. But only providing the assignment form strikes me as over-specialization. Operators are a straightforward generalization of assignments. When you specialize the syntax to provide an assignment form only, you force people to create temporary variables when they aren't needed. Compound expressions are good things!
>
>
> And why break the pattern of compound assignments being based on binary operators? Why should ?= or ??= be different from +=, -=, *=, /=, %=,<<=,>>=,>>>=,&=, |=, ^=? It just seems like a pointless restriction.
>
>
> Dave
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
More information about the es-discuss
mailing list