User-reifiable References

Claus Reinke claus.reinke at
Tue Jul 31 15:33:09 PDT 2012

> As noted, we're moving away from References in the language.

l-values as function parameters are still going to be useful,
for abstractions involving assignment operators.

> Macros are out there but the better way: you want to abstract over syntax, not runtime lvalue 
> semantics.

Useful macros are going to have a lot of expressive power
over syntax, which tends to go along with decreased ability
to reason about code. Functions are not as simple in JS as
they are in other functional languages, but it still seems
advisable to do as much as possible without macros.

I've updated my infix operator gist, to include initial support
for 'ref', and also to wrap operator arguments - so both
assignment operators and short-circuiting operators are
now possible. Unwrapping of arguments and reference
objects is done explicitly in operator definitions.

See example 6 (strawman:default_operator, .??, .??=) in

The expected output (for those who don't want to clone and run
the gist) can be seen in the comments of

In this gist, the desugaring of 'ref' and infix operators corresponds
to hard-coded macros, doing the things with syntax that cannot
be done with functions alone. The rest is handled by function
definitions and conventional code.

Until macros make it into the language, this is an easy way
to play with suggested operators (factoring out the parser
hacking, some of which is still TODO).

Example extract:

     Operator['.??='] =
          var lhs   = lhs_();
          var value = lhs.deref;
          return (lhs.deref = ( value!==undefined ? value : rhs_()))

      var x;
      console.log('6(assignment x):', ref x .??= 1);
      console.log('6(assignment x):', x );

Output extract:

    6(assignment x): 1
    6(assignment x): 1

Btw, I've also added (example 7) a simple version of Allen's new 
strawman:define_properties_operator, '.:=' .


More information about the es-discuss mailing list