New private names proposal
Mark S. Miller
erights at google.com
Wed Dec 22 12:31:08 PST 2010
On Wed, Dec 22, 2010 at 11:56 AM, Mark S. Miller <erights at google.com> wrote:
> On Tue, Dec 21, 2010 at 2:44 PM, Allen Wirfs-Brock <allen at wirfs-brock.com>
> wrote:
>
>>
>> Please don't totally disengage from the syntax discussion. Most
>> programmers understanding of the language starts with the concrete (syntax)
>> and then proceeds to the abstract (semantics). Syntax design can have a big
>> impact on the usability of the underlying semantics
>>
>
>
> Ok, I am not sure what I think of the following idea, but it's a bit
> different in flavor and so may stimulate other thoughts. I will express the
> expansion in terms of the natural expansion for a soft fields underpinning.
> One could do an equally natural expansion for private names. "==>" means
> "expands to". Actual expansions would be a bit more complex to preserve the
> left-to-right order of evaluation of the original.
>
> The basic idea is, since we're considering a sigil anyway, and since .# and
> [# would both treat the thing to their right as something to be evaluated,
> why not turn the sigil into an infix operator instead? Then it can be used
> as "."-like "[]"-like without extra notation or being too closely confused
> with "." or "[]" themselves. Finally, given the meaning of the sigil-turned
> operator, "@" seemed to read better to me than "#". YMMV.
>
>
> expr1 @ expr2
> ==>
> expr2.get(expr1)
>
> expr1 @ expr2 = expr3;
> ==>
> expr2.set(expr1, expr3);
>
> const obj = {@expr1: expr2, ...};
> ==>
> const obj = {...}; expr1.set(obj, expr2);
>
>
> Perhaps the expression on the right need not be a Name/SoftField. It could
> be anything that responds to "get" and "set".
>
>
Redoing the "class private" example at the end of
http://wiki.ecmascript.org/doku.php?id=strawman:private_names#using_private_identifiers
http://wiki.ecmascript.org/doku.php?id=strawman:names_vs_soft_fields#using_private_identifiers
const key = SoftField(); // or, obviously, Name(),
depending...function Thing() {
this @ key = "class private value";
this.hasKey = function(x) {
return x @ key === this @ key;
};
this.getThingKey = function(x) {
return x @ key;
};}
var thing1 = new Thing;var thing2 = new Thing;
print("key" in thing1); // falseprint(thing1.hasKey(thing1));
// trueprint(thing1.hasKey(thing2)); // true
--
Cheers,
--MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20101222/547ad51b/attachment.html>
More information about the es-discuss
mailing list