default constructors and implicit super() insertion

Brendan Eich brendan at mozilla.org
Fri Jun 22 18:41:03 PDT 2007


On Jun 22, 2007, at 3:11 PM, P T Withington wrote:

> I've always wondered what the point of carrying over C++'s
> constructor syntax was.

Ignoring the old pre-historic ES4 spec cited by Peter, I have to  
apologize again for:

http://developer.mozilla.org/es4/proposals/nullability.html

being out of date. The syntax we've settled on is not C++-like to  
that degree. It looks like this:

class C {
     var x : Object!
     var y : int

     function C(initialX : Object!, initialY : int)
       : x = initialX, y = initialY
     {
         ...
     }
}

A few points:

1. The purpose of this syntax is to ensure that class vars of non- 
nullable type are known to be initialized, with no chance for an  
uninitialized error that would be the dual of the null pointer  
exception (the exception that we hope to avoid via non-nullable  
types), while not requiring definite assignment or any other such  
analysis.

2. The = assignment operator is used as elsewhere, to allow  
destructuring assignments to set non-nullable vars. To avoid  
confusion with other kinds of initialization, we call these  
assignment expressions in the head of the constructor "settings",  
"class settings", or "constructor settings".

3. The scope chain for the right-hand side of each = includes the  
formal parameter names but not the new |this| object, while the scope  
chain for the left-hand side includes |this| but not the formal  
parameters. So the above could be simplified:

class C {
     var x : Object!
     var y : int

     function C(x : Object!, y : int) : x = x, y = y
     {
         ...
     }
}

4. An alternative syntax inspired by OCaml was:

class C(ax : Object!, ay : int) {
     var x : Object! = ax;
     var y : int = ay;

     function C {
          ...
     }
}

but this is too far from existing syntax and scoping (see http:// 
developer.mozilla.org/es4/discussion/nullability.html).

Anyway, settings are implemented in the reference implementation  
available at http://ecmascript-lang.org/ -- try them out and file  
bugs at the trac if need be (look for existing tickets on the same  
issue first). Thanks,

/be




More information about the Es4-discuss mailing list