Block exprs as better object literals (was: Semantics and abstract syntax of lambdas)
Yuh-Ruey Chen
maian330 at gmail.com
Mon Dec 22 13:35:28 PST 2008
Mark S. Miller wrote:
> If a block contains revealed declarations, then the block's revealed
> value is a new non-extensible object whose properties mirror these
> declared variable names. A revealed "const" is simply a copy of the
> same value. For "var", "let", and "function", the property is an
> accessor property without a setter, whose getter gets the named
> variable. Revealed "function"s and the last (method) production create
> non-enumerable properties. The others are enumerable. Revealed "var"s
> create configurable properties. The others are non-configurable. Thus,
> in the absence of a revealed "var", the revealed object is frozen.
> Redoing Peter's example[2], we get
>
> const Point = lambda(privX, privY) {
> let privInstVar = 2;
> const privInstConst = -2;
> reveal toString() { reveal ('<' + getX() + ',' + getY() + '>') };
> reveal getX() { reveal privX };
> reveal getY() { reveal privY };
> reveal let pubInstVar = 4;
> reveal const pubInstConst = -4;
> }
>
> Revealed declarations always desugar to object creation + property
> definition + revealing the created object. So the above example would
> desugar to
>
> const Point = lambda(privX, privY) {
> let privInstVar = 2;
> const privInstConst = -2;
> const toString = lambda() { reveal ('<' + getX() + ',' + getY() +
> '>') };
> const getX = lambda() { reveal (privX) };
> const getY = lambda() { reveal (privY) };
> let pubInstVar = 4;
> const pubInstConst = -4;
> reveal (Object.preventExtensions(Object.create(Object.prototype, {
> toString: {value: toString},
> getX: {value: getX},
> getY: {value: getY},
> pubInstVar: {get: lambda{reveal (pubInstVar)}, enumerable: true},
> pubInstConst: {value: pubInstConst, enumerable: true}
> })))
> }
TBH, even after substituting more Java-esque keywords like "public" for
"reveal", this just doesn't appeal to my aesthetic sense. The user will
naturally wonder why he should do |reveal (privX)| rather than |return
privX|. I'm not sure what the benefit there is to trying to completely
replace functions with lambdas for class methods.
More information about the Es-discuss
mailing list