New private names proposal
Brendan Eich
brendan at mozilla.com
Tue Dec 21 17:57:22 PST 2010
On Dec 21, 2010, at 5:13 PM, David Flanagan wrote:
> On 12/21/2010 04:25 PM, Brendan Eich wrote:
>>
>> Why does your expectation differ here compared to the following:
>>
>> MyAwesomeThing.prototype.myCoolFunction = function() {
>> var cachedHotness = gensym();
>> if (!this[cachedHotness])
>> this[cachedHotness] = doExpensiveThing(this)
>> return this[cachedHotness];
>> }
>>
>> Is it because |private cachedHotness;| does not "look generative"?
>>
>
> I agree with Oliver: the private keyword is going to cause confusion. It looks like it is declaring something, not generating something.
It's a fair objection for sure.
Mark made a different objection in http://wiki.ecmascript.org/doku.php?id=strawman:inherited_explicit_soft_fields#can_we_subsume_names: "I (MarkM) do not like the sugar proposed for Names, as I think it encourages confusion between literal text and lexical lookup."
That is, without private_names, the "p" in the expression |o.p| is literal text, not lexically bound as it could be via a prior |private p|. This too is a fair objection, not an absolute as Mark's words convey, rather an informed opinion given with pretty-clear reason backing it.
So (must remember to say this), thanks to Oliver and Mark for these objections (and to David for amplifying).
> A small step toward making the proposed syntax less Java-like (and therefore less likely to cause confusion) might be:
>
> use private cachedHotness;
We have that kind of syntax reserved for pragmas:
http://wiki.ecmascript.org/doku.php?id=strawman:pragmas
It seems even less generative, since pragmas are typically compile-time (or compile-and-runtime, but at least compile-time).
> A use directive feels vaguely more comfortable here to me. It makes it clearer to the programmer that some kind of magic is going on.
>
> But I confess that I haven't actually read Allen's proposal, so take this with a grain of salt.
What's generative in JS already? Mutable object "literals":
function f() {} // inside the outer myCoolFunction
var o = {p:1};
var a = [1,2];
var r = /hi/; // thanks to ES5 recognizing reality over ES3
That suggests the obvious:
private cachedHotness = gensym();
but then the gensym() initializer, which does indeed scream "new generated value here on every evaluation!", becomes both proper notice and (over time) deadwood, boilerplate. Plus, what's gensym calling? Maybe that built-in is shadowed.
We could try shortening and using a keyword for unambiguous and novel generativity-here notice to readers:
private cachedHotness = new;
I admit, it looks weird. Lose the =, try for special-form syntax like a function declaration uses:
private cachedHotness {};
Does this "look generative"?
How about a "more special" special form:
new private cachedHotness;
Not generative-looking enough?
Discussion list fodder, glad we have es-discuss for this stuff.
/be
More information about the es-discuss
mailing list