logical-assignment operators

Steve Yegge steve.yegge at gmail.com
Mon Feb 12 12:11:26 PST 2007

Hi folks,

A little bird told me that TG1 may have dropped support for the assignment
operators &&=, ||=, ^^= due to lack of popular demand and/or compelling use

If so, I thought I'd lobby to get at least ||= added back in.  I don't know
of any
really good uses for &&= or ^^=, but ||= is being popularized by Ruby and,
particular, Rails, as a nice shorthand for setting a default value.  So you
things like this:

  foo.bar ||= default_value;

instead of:

  foo.bar = foo.bar || default_value;

Given that the lvalue expression can often be quite long, it can add up to a
significant compression.

A quick grep on the Rails trunk shows ||= is used 317 times.  Rails
keyword parameters by passing around hashes of optional arguments.  This
of course yields functions that have hundreds of possible code paths, and to
consolidate them, the entry-point functions all ||= in default values.

Note that they also make use of the expression's return value, e.g.:

web_service(:one) { @service_one ||= TestInvokeService.new }


 (@signal_handlers ||= Hash.new)[signal] = handler

In the latter case, it was a succinct way to lazily initialize an instance
before assigning to it.

I think we're going to see a lot of Ruby code ported to JavaScript when ES4
hits the big platforms.  It's just syntactic sugar, but it's such a common
case (and presumably really easy to support) that I'd vote for including it.


Steve Yegge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20070212/ae514fb8/attachment.html 

More information about the Es4-discuss mailing list