||= is much needed?

classic Classic list List threaded Threaded
74 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

||= is much needed?

Hemanth H.M
Would something like :

obj["prop"] ||= "NewProp"

be useful? 

--
'I am what I am because of who we all are'
h3manth.com
-- Hemanth HM

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

David Bruant-5
Le 12/06/2012 15:27, Hemanth H.M a écrit :
Would something like :

obj["prop"] ||= "NewProp"

be useful?
ahah, I asked the same question very recently [1]. Answer by Brendan Eich [2].
I definitely agree that default arguments are a decent alternative. I can't recall examples where it wouldn't be enough. Do you have use cases where you would use ||= and default argument values couldn't be used?

David

[1] https://twitter.com/DavidBruant/status/210654806732324864
[2] https://twitter.com/BrendanEich/status/210750515808706561

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Rick Waldron


On Tue, Jun 12, 2012 at 10:33 AM, David Bruant <[hidden email]> wrote:
Le 12/06/2012 15:27, Hemanth H.M a écrit :
Would something like :

obj["prop"] ||= "NewProp"

be useful?

There is currently a strawman proposal for the "Default Operator", which can be found here:



Rick

 
ahah, I asked the same question very recently [1]. Answer by Brendan Eich [2].
I definitely agree that default arguments are a decent alternative. I can't recall examples where it wouldn't be enough. Do you have use cases where you would use ||= and default argument values couldn't be used?

David

[1] https://twitter.com/DavidBruant/status/210654806732324864
[2] https://twitter.com/BrendanEich/status/210750515808706561

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Hemanth H.M
Kool, well was looking into use cases....well my first frustration was [1]

I'm new to this, how does one write proposals? Is it a wiki? 

[1] https://twitter.com/GNUmanth/status/208555914733682690

On Tue, Jun 12, 2012 at 8:36 PM, Rick Waldron <[hidden email]> wrote:
http://wiki.ecmascript.org/doku.php?id=strawman:default_operator



--
'I am what I am because of who we all are'
h3manth.com
-- Hemanth HM

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Rick Waldron
In reply to this post by Rick Waldron


On Tue, Jun 12, 2012 at 11:45 AM, Ryan Florence <[hidden email]> wrote:
I use ||= very regularly in ruby and coffeescript, both of which have default arguments.

I definitely agree that default arguments are a decent alternative. I can't recall examples where it wouldn't be enough. Do you have use cases where you would use ||= and default argument values couldn't be used?

Its super handy for caching and late/dynamic initialization of object properties.


var events = {

  _callbacks: {},

  on: function (topic, callback) {
    (this._callbacks[topic] ||= []).push(callback);
    ...
  },
  ...
};

It's also useful for settings object defaults, especially for expensive settings that you don't want to calculate in some `defaults` object that is merged into the settings.

function ajaxWithError (settings) {
  settings.method ||= 'POST';

  settings.elementPosition ||= this._getElementPositions();

  settings.somethingWithInitialization ||= (function(){
    var thing = new Thing();
    thing.foo = settings.foo;
    return thing;
  })();
  ...
};

The ||= (function(){})() should remind any rubyist of `||= begin ... end` which is beloved by many.

So yeah, I think its really useful outside of default arguments.

- Ryan Florence


Forgive the dogpiling, but these are really excellent real-world use cases that represent massive pain points in modern JavaScript programming. Thanks for writing these up Ryan, I've added them to strawman: http://wiki.ecmascript.org/doku.php?id=strawman:default_operator

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

AJ ONeal-3
+1

I desperately want this in the language.

AJ ONeal

On Tue, Jun 12, 2012 at 11:14 AM, Rick Waldron <[hidden email]> wrote:


On Tue, Jun 12, 2012 at 11:45 AM, Ryan Florence <[hidden email]> wrote:
I use ||= very regularly in ruby and coffeescript, both of which have default arguments.

I definitely agree that default arguments are a decent alternative. I can't recall examples where it wouldn't be enough. Do you have use cases where you would use ||= and default argument values couldn't be used?

Its super handy for caching and late/dynamic initialization of object properties.


var events = {

  _callbacks: {},

  on: function (topic, callback) {
    (this._callbacks[topic] ||= []).push(callback);
    ...
  },
  ...
};

It's also useful for settings object defaults, especially for expensive settings that you don't want to calculate in some `defaults` object that is merged into the settings.

function ajaxWithError (settings) {
  settings.method ||= 'POST';

  settings.elementPosition ||= this._getElementPositions();

  settings.somethingWithInitialization ||= (function(){
    var thing = new Thing();
    thing.foo = settings.foo;
    return thing;
  })();
  ...
};

The ||= (function(){})() should remind any rubyist of `||= begin ... end` which is beloved by many.

So yeah, I think its really useful outside of default arguments.

- Ryan Florence


Forgive the dogpiling, but these are really excellent real-world use cases that represent massive pain points in modern JavaScript programming. Thanks for writing these up Ryan, I've added them to strawman: http://wiki.ecmascript.org/doku.php?id=strawman:default_operator

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
In reply to this post by Rick Waldron
Rick Waldron wrote:
> On Tue, Jun 12, 2012 at 11:45 AM, Ryan Florence <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I use ||= very regularly in ruby and coffeescript, both of which
>     have default arguments.
>

I don't see Ryan's mail.

First, as we've discussed in the past, ||= in Ruby is not what we've
proposed. In JS, assignment operators expand like so

   A op= B;  ~~>  A = A op B;

with of course a temporary to hold the base of A (which must evaluate to
a Reference) so side effects are not duplicated.

In Ruby IIRC, A ||= B is A = B unless A (hope I have this right).

This matters if A is an accessor. Do we always set A, even to its
current value if truthy?

>
>>         I definitely agree that default arguments are a decent
>>         alternative. I can't recall examples where it wouldn't be
>>         enough. Do you have use cases where you would use ||= and
>>         default argument values couldn't be used?
>
>     Its super handy for caching and late/dynamic initialization of
>     object properties.
>
>
>         var events = {
>
>           _callbacks: {},
>
>           on: function (topic, callback) {
>             (this._callbacks[topic] ||= []).push(callback);
>             ...
>           },
>           ...
>         };
>

This is handy, and also idiomatic but to multiple languages.

It's worth considering, but we need to wrestle the semantics to the
ground. If the left-hand side's value is truthy, I argue there should be
no useless assignment of that value to the left-hand side.

IOW I favor Ruby semantics. This breaks from JS's C-inspired assignment
operators, but perhaps we can live with it.

/be
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Wes Garland
> This breaks from JS's C-inspired assignment operators, but perhaps we can live with it.

FWIW -- I was confused when I first read ||=, I thought it was supposed to be some kind of Boolean-coercing variant on |=. Now I see that it is more like ?= from GNU make.

What do you think of GCC's ?: operator? It is basically a special form of the ternary operator, and while not the same as your propsal, it addresses many of the same use cases.

(I've been happy with it for a long time).

Wes

--
Wesley W. Garland
Director, Product Development
PageMail, Inc.
+1 613 542 2787 x 102

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Wes Garland wrote:
> > This breaks from JS's C-inspired assignment operators, but perhaps
> we can live with it.
>
> FWIW -- I was confused when I first read ||=, I thought it was
> supposed to be some kind of Boolean-coercing variant on |=. Now I see
> that it is more like ?= from GNU make.

We could use ?= instead. Good point. This avoids the dual semantics for
A op= B split on whether op is || (an existing operator, unlike lone ?).

> What do you think of GCC's ?: operator? It is basically a special form
> of the ternary operator, and while not the same as your propsal, it
> addresses many of the same use cases.

That's ok as an alternative to ?? but would you want ?:= as the
assignment form? I'd rather lose the colon.

Given ?=, people may wish for A ? B instead of A ?? B, but then we have
nasty issues with respect to ternary:

A ? B ? C : D

Is this (A ? B) ? C : D or A ? (B ? C) : D. We can disambiguate in the
formal grammar but readers may rebel.

It's possible ?? or however we spell it isn't worth adding, while ?= is.
The conditional assignment to default or normalize is the prime use-case.

Even then, we have lingering debates over falsy vs. null-or-undefined
vs. undefined only. CoffeeScript does null-or-undefined, IIRC.

/be

>
> (I've been happy with it for a long time).
>
> Wes
>
> --
> Wesley W. Garland
> Director, Product Development
> PageMail, Inc.
> +1 613 542 2787 x 102
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Brendan Eich wrote:
> Is this (A ? B) ? C : D or A ? (B ? C) : D. We can disambiguate in the
> formal grammar but readers may rebel.

Or A ? (B ? C : D), of course.

Just say no to lone ? as new operator. I'm warming up to ?= though!

/be
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Tom Ellis-3
I like the sound of ?= too.

var a;

//later on

a ?= 15;

It goes with all the other operators that are out there too (!=, =, ==, === etc).

Tom

On 12 Jun 2012, at 19:04, Brendan Eich wrote:

> Brendan Eich wrote:
>> Is this (A ? B) ? C : D or A ? (B ? C) : D. We can disambiguate in the formal grammar but readers may rebel.
>
> Or A ? (B ? C : D), of course.
>
> Just say no to lone ? as new operator. I'm warming up to ?= though!
>
> /be
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Russell Leggett
On Tue, Jun 12, 2012 at 2:31 PM, Tom Ellis <[hidden email]> wrote:
I like the sound of ?= too.

var a;

//later on

a ?= 15;

It goes with all the other operators that are out there too (!=, =, ==, === etc).

+1, useful and intuitive.

- Russ
 

Tom

On 12 Jun 2012, at 19:04, Brendan Eich wrote:

> Brendan Eich wrote:
>> Is this (A ? B) ? C : D or A ? (B ? C) : D. We can disambiguate in the formal grammar but readers may rebel.
>
> Or A ? (B ? C : D), of course.
>
> Just say no to lone ? as new operator. I'm warming up to ?= though!
>
> /be
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Russell Leggett wrote:

> On Tue, Jun 12, 2012 at 2:31 PM, Tom Ellis <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     I like the sound of ?= too.
>
>     var a;
>
>     //later on
>
>     a ?= 15;
>
>     It goes with all the other operators that are out there too (!=,
>     =, ==, === etc).
>
>
> +1, useful and intuitive.

And (to be clear) the semantics for

LeftHandSideExpression ?= AssignmentExpression

are roughly

Let lref = evaluate A.
Let lval = GetValue(lref).
Let rref = evaluate B.
Let rval = GetValue(rref).
Throw a SyntaxError exception if the following conditions are all true:
• Type(lref) is Reference is true
• IsStrictReference(lref) is true
• Type(GetBase(lref)) is Environment Record
• GetReferencedName(lref) is either "eval" or "arguments"
If lval is undefined, call PutValue(lref, rval).

to assign the default value if and only if the left-hand side's current
value is undefined.

/be

>
> - Russ
>
>
>     Tom
>
>     On 12 Jun 2012, at 19:04, Brendan Eich wrote:
>
>     > Brendan Eich wrote:
>     >> Is this (A ? B) ? C : D or A ? (B ? C) : D. We can disambiguate
>     in the formal grammar but readers may rebel.
>     >
>     > Or A ? (B ? C : D), of course.
>     >
>     > Just say no to lone ? as new operator. I'm warming up to ?= though!
>     >
>     > /be
>     > _______________________________________________
>     > es-discuss mailing list
>     > [hidden email] <mailto:[hidden email]>
>     > https://mail.mozilla.org/listinfo/es-discuss
>
>     _______________________________________________
>     es-discuss mailing list
>     [hidden email] <mailto:[hidden email]>
>     https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Brendan Eich wrote:
> LeftHandSideExpression ?= AssignmentExpression
>
> are roughly
>
> Let lref = evaluate A.
> Let lval = GetValue(lref).
> Let rref = evaluate B.

Of course, A should be LeftHandSideExpression and B should be
AssignmentExpression.

This is pretty simple. We could even grant an exception and get it into
ES6, IMHO. But first it needs a strawman and some discussion.

Absent fast objections here, I'll co-opt the

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

strawman and put it on the agenda.

/be
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Updated. Sorry, no ||| instead of ??. Instead, inspired by Wes's GCC
reminder, I kept the ?? operator but re-spelled it as ?: and made ??= be
spelled ?=.

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

The semantics match only undefined. APIs use null as no-object and do
not want to trigger defaulting, here or with parameter default values.

This reminds me: IIRC we need to agree that an explicit undefined actual
argument triggers defaulting for parameter default values (as it would
if the pdv-based code were rewritten to use ?: or ?=).

/be

Brendan Eich wrote:

> Brendan Eich wrote:
>> LeftHandSideExpression ?= AssignmentExpression
>>
>> are roughly
>>
>> Let lref = evaluate A.
>> Let lval = GetValue(lref).
>> Let rref = evaluate B.
>
> Of course, A should be LeftHandSideExpression and B should be
> AssignmentExpression.
>
> This is pretty simple. We could even grant an exception and get it
> into ES6, IMHO. But first it needs a strawman and some discussion.
>
> Absent fast objections here, I'll co-opt the
>
> http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
>
> strawman and put it on the agenda.
>
> /be
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

John Tamplin
In reply to this post by Brendan Eich-2
On Tue, Jun 12, 2012 at 3:09 PM, Brendan Eich <[hidden email]> wrote:
And (to be clear) the semantics for

LeftHandSideExpression ?= AssignmentExpression

are roughly

Let lref = evaluate A.
Let lval = GetValue(lref).
Let rref = evaluate B.
Let rval = GetValue(rref).
Throw a SyntaxError exception if the following conditions are all true:
• Type(lref) is Reference is true
• IsStrictReference(lref) is true
• Type(GetBase(lref)) is Environment Record
• GetReferencedName(lref) is either "eval" or "arguments"
If lval is undefined, call PutValue(lref, rval).

to assign the default value if and only if the left-hand side's current value is undefined.

Wouldn't you want B to be evaluated only if A is undefined? 

--
John A. Tamplin
Software Engineer (GWT), Google

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Allen Wirfs-Brock

On Jun 12, 2012, at 3:26 PM, John Tamplin wrote:

On Tue, Jun 12, 2012 at 3:09 PM, Brendan Eich <[hidden email]> wrote:
And (to be clear) the semantics for

LeftHandSideExpression ?= AssignmentExpression

are roughly

Let lref = evaluate A.
Let lval = GetValue(lref).
Let rref = evaluate B.
Let rval = GetValue(rref).
Throw a SyntaxError exception if the following conditions are all true:
• Type(lref) is Reference is true
• IsStrictReference(lref) is true
• Type(GetBase(lref)) is Environment Record
• GetReferencedName(lref) is either "eval" or "arguments"
If lval is undefined, call PutValue(lref, rval).

to assign the default value if and only if the left-hand side's current value is undefined.

Wouldn't you want B to be evaluated only if A is undefined? 

Yes, that makes it consistent with the semantics of ? :

Allen



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
In reply to this post by John Tamplin
Heh, I did write *roughly* :-P.

Let lref = evaluate LeftHandSideExpression.
Let lval = GetValue(lref).
Throw a SyntaxError exception if the following conditions are all true:
   • Type(lref) is Reference is true
   • IsStrictReference(lref) is true
   • Type(GetBase(lref)) is Environment Record
   • GetReferencedName(lref) is either "eval" or "arguments"
If lval is undefined, then:
    Let rref = evaluate AssignmentExpression.
    Let rval = GetValue(rref).
   Call PutValue(lref, rval).

Thanks, fixing and transcribing into

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

/be

John Tamplin wrote:

> On Tue, Jun 12, 2012 at 3:09 PM, Brendan Eich <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     And (to be clear) the semantics for
>
>     LeftHandSideExpression ?= AssignmentExpression
>
>     are roughly
>
>     Let lref = evaluate A.
>     Let lval = GetValue(lref).
>     Let rref = evaluate B.
>     Let rval = GetValue(rref).
>     Throw a SyntaxError exception if the following conditions are all
>     true:
>     • Type(lref) is Reference is true
>     • IsStrictReference(lref) is true
>     • Type(GetBase(lref)) is Environment Record
>     • GetReferencedName(lref) is either "eval" or "arguments"
>     If lval is undefined, call PutValue(lref, rval).
>
>     to assign the default value if and only if the left-hand side's
>     current value is undefined.
>
>
> Wouldn't you want B to be evaluated only if A is undefined?
>
> --
> John A. Tamplin
> Software Engineer (GWT), Google
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Brendan Eich-2
Ok, that (cited below) was wrong too.

This should be right:
http://wiki.ecmascript.org/doku.php?id=strawman:default_operator#semantics

Comments welcome. Thanks again, I needed some caffeine!

/be

Brendan Eich wrote:

> Heh, I did write *roughly* :-P.
>
> Let lref = evaluate LeftHandSideExpression.
> Let lval = GetValue(lref).
> Throw a SyntaxError exception if the following conditions are all true:
>   • Type(lref) is Reference is true
>   • IsStrictReference(lref) is true
>   • Type(GetBase(lref)) is Environment Record
>   • GetReferencedName(lref) is either "eval" or "arguments"
> If lval is undefined, then:
>    Let rref = evaluate AssignmentExpression.
>    Let rval = GetValue(rref).
>   Call PutValue(lref, rval).
>
> Thanks, fixing and transcribing into
>
> http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
>
> /be
>
> John Tamplin wrote:
>> On Tue, Jun 12, 2012 at 3:09 PM, Brendan Eich <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     And (to be clear) the semantics for
>>
>>     LeftHandSideExpression ?= AssignmentExpression
>>
>>     are roughly
>>
>>     Let lref = evaluate A.
>>     Let lval = GetValue(lref).
>>     Let rref = evaluate B.
>>     Let rval = GetValue(rref).
>>     Throw a SyntaxError exception if the following conditions are all
>>     true:
>>     • Type(lref) is Reference is true
>>     • IsStrictReference(lref) is true
>>     • Type(GetBase(lref)) is Environment Record
>>     • GetReferencedName(lref) is either "eval" or "arguments"
>>     If lval is undefined, call PutValue(lref, rval).
>>
>>     to assign the default value if and only if the left-hand side's
>>     current value is undefined.
>>
>>
>> Wouldn't you want B to be evaluated only if A is undefined?
>>
>> --
>> John A. Tamplin
>> Software Engineer (GWT), Google
>> _______________________________________________
>> es-discuss mailing list
>> [hidden email]
>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: ||= is much needed?

Dave Herman
In reply to this post by Brendan Eich-2
On Jun 12, 2012, at 11:02 AM, Brendan Eich wrote:

> It's possible ?? or however we spell it isn't worth adding, while ?= is. The conditional assignment to default or normalize is the prime use-case.

I'm skeptical. You don't foresee

    f(obj.x ?? defVal)

happening a lot? I do.

Dave

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
1234