Block exprs as better object literals (was: Semantics and abstract syntax of lambdas)

Mark Miller erights at gmail.com
Mon Jan 5 04:50:10 PST 2009


2009/1/5 Mark S. Miller <erights at google.com>:
> <member> ::= [...]
> |        "public" <ident> <paramList> <block>
>
> <declaration> ::= ...
> |        "class" <ident> <paramList> <objectBody>

Both classes and methods should have the pleasant property of
functions, that their initialization is hoisted to the beginning of
the enclosing block, so there is no dead zone read barrier. This means
my desugaring should be

const Point = Object.freeze(function(privX, privY) {
  return let {
    const toString = Object.freeze(function() {
      return ('<' + getX() + ',' + getY() + '>');
    });
    const getX = Object.freeze(function() { return privX; });
    const getY = Object.freeze(function() { return privY; });
    let privInstVar = 2;
    const privInstConst = -2;
    let pubInstVar = 4;
    const pubInstConst = -4;
    Object.freeze(Object.create(Point.prototype, {
      toString: {value: toString},
      getX: {value: getX},
      getY: {value: getY},
      pubInstVar: {get: function{return pubInstVar;},
                   enumerable: true},
      pubInstConst: {value: pubInstConst,
                     enumerable: true}
    }))
  };
});

where that desuagring as a whole is likewise hoisted.

Btw, if we leave out the "object" production and desugar the "class"
production directly, then we also avoid the need to solve the
blocks-as-expression problem, and the desugaring above simplifies to

const Point = Object.freeze(function(privX, privY) {
  const toString = Object.freeze(function() {
    return ('<' + getX() + ',' + getY() + '>');
  });
  const getX = Object.freeze(function() { return privX; });
  const getY = Object.freeze(function() { return privY; });
  let privInstVar = 2;
  const privInstConst = -2;
  let pubInstVar = 4;
  const pubInstConst = -4;
  return Object.freeze(Object.create(Point.prototype, {
    toString: {value: toString},
    getX: {value: getX},
    getY: {value: getY},
    pubInstVar: {get: function{return pubInstVar;},
                 enumerable: true},
    pubInstConst: {value: pubInstConst,
                   enumerable: true}
  }));
});

However, this leaves no way to enable the object to refer to itself
without inventing further syntax.

-- 
Text by me above is hereby placed in the public domain

    Cheers,
    --MarkM


More information about the Es-discuss mailing list