default constructors and implicit super() insertion

Brendan Eich brendan at mozilla.org
Tue Jun 26 17:01:28 PDT 2007


On Jun 26, 2007, at 4:43 PM, Sho Kuwamoto wrote:

> Just curious...
>
> In modern compilers, can't the compiler issue an error if the
> non-nullable member was (a) not initialized in the constructor, or (b)
> used before it was set?

See the bit I wrote about "definite assignment ... analysis". Indeed  
modern compilers can do many analyses, but this is JavaScript. It has  
to be easy to implement on cell phones. It cannot spend a lot of  
time, even ignoring device-imposed code size constraints, in  
compiling downloaded code.

> I've always hated the C++-style member initialization syntax and it
> seems like a shame to add it if it could be avoided.

C++-style member initialization is being avoided. That is, you don't  
see 'C(ax, ay) : x(ax), y(ay) {}' in the constructor. It's true that  
we are using : after the parameter list of the constructor function,  
but that's unambiguous (because you can't write a return type  
annotation for a constructor).

So, what do you mean, if not the : as the punctuator that introduces  
the settings? And why the hate? This corner case with one character  
in common with C++ syntax does not make the whole case "C++-style  
member initialization".

> Shouldn't it also
> be a goal to make sure that ES4 is less baroque and easier to learn  
> than
> C++?

Remember that these "settings" are required only if you use non- 
nullable types for class variables that must be initialized.

Asking a question that smuggles your (unjustified) conclusion that  
this particular corner case is "baroque" is no fair. Let's define  
baroque carefully, without emotion ("hate") and whipping-devils (C++).

/be

>
> Just my 2c.
>
> -Sho
>
>
>
> -----Original Message-----
> From: es4-discuss-bounces at mozilla.org
> [mailto:es4-discuss-bounces at mozilla.org] On Behalf Of Brendan Eich
> Sent: Friday, June 22, 2007 6:41 PM
> To: P T Withington
> Cc: Peter Hall; es4-discuss
> Subject: Re: default constructors and implicit super() insertion
>
> 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
>
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
> _______________________________________________
> Es4-discuss mailing list
> Es4-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss




More information about the Es4-discuss mailing list