Minimalist Classes

David Flanagan dflanagan at mozilla.com
Wed Nov 2 15:08:52 PDT 2011


On 11/2/11 11:16 AM, Brendan Eich wrote:
> On Nov 2, 2011, at 10:52 AM, David Flanagan wrote:
>
>
>>   Could they have initializers that were automatically included in 
>> the constructor?
>
> I answered that in a gist comment. Yes, I like the CoffeeScript 
> constructor(@x, @y){} shorthand. Dart picked up on it, but requires 
> this. instead of @. I'll put it in the gist.
>
>
I saw the comment, I think.  What I meant here was initializer 
expressions that are part of the 'private' declaration.  That is, can I 
write

     private stack = [];

and have that initialization code automatically inserted (Java-like) 
into the constructor for me?  I'm trying to understand whether private 
and static have the same basic syntax in this proposal.

>>   Plain 'x' is bound to the Name object and @x is the value of the 
>> property with that name?  Is x a variable?
>
> x is a const binding in the "@ scope" of the class body. It does not 
> collide with any lexically bound x (formal parameter name, e.g.). 
> That's important -- see the Monster constructor with its name and 
> health parameters and private property names.
>
> So, o at x is *not* o[x] for such a private-declared x.
>
What I don't understand is whether the "@ scope" is a specification 
abstraction, or whether it is visible to programmers.  If I declare a 
private variable x, and there isn't any other x in scope, is x bound to 
a Name object that I can use?

If not, is there any way I can access the automatically-created Name 
objects so that I can share my private names with "friend" classes?

> Some argue @ should work for any x, so if there were some x = "y" in 
> scope, then o at x would be o[x] would be o.y. I think that's too 
> error-prone. What if I typo @heath instead of @health and there's a 
> non-private-declared heath in scope?
>
That's where a 'public' declaration comes in!  'private x' binds x to a 
new Name in @ scope.  And 'public y' binds y to "y" in @ scope.  Then 
make @ work with any identifier declared private or public.  That would 
allow programmers to elide the "this" but still have publicly visible 
properties.

Resolution of identifiers in @ scope can happen at compile time, can't 
it?  That is, for the code "@x", x is just looked up in @ scope once 
when the code is compiled, not dynamcally each time it is evaluated, right?

     David


More information about the es-discuss mailing list