default constructors and implicit super() insertion

Sho Kuwamoto skuwamot at adobe.com
Wed Jun 27 11:13:39 PDT 2007


Lars T Hansen wrote:
>I think Brendan has an important point in that non-nullable 
>initializers will be clearly visible.  Non-nullability is 
>both strange and hard to use, and making it syntactically 
>special is not obviously bad.

>Speaking as someone who's written a lot of ES4 code so far 
>(and a lot of C++ before that), the settings are very useful 
>because they allow the constructor arguments to be named 
>with the same names as the instance properties for which 
>they provide initial values, idiomatically
>
>  class C {
>    function C(x, y) : x=x, y=y {}
>    var x, y;
>  }
>

While I see that it can be useful, I remain concerned that this will
seem confusing to novices. Why do I need that colon? What does "x=x"
mean? Why did I get the wrong result when I did the following, which
looks a lot like the above?

  // novice error 1
  class C {
    function C(x, y) {
      x=x;
      y=y;
    }
    var x, y;
  }

What happens when an initialization requires a bit of logic? Suppose I
have a "title" property of a MyDocument object that is non-nullable. If
the name is passed in, it is used. Otherwise, one is generated (e.g.,
"Untitled1").

I'm not really an expert, but in terms of overall simplicity, my
preference is

   a) provide a clear way to disambiguate between members 
      and other variables in scope in *all* cases. (this may 
      already exist)

   b) allow non-nullable types to be initialized in the 
      constructor in the body of the constructor, as 
      opposed to creating new syntax

   c) make it a *runtime* error to read an unitialized 
      non-nullable object. also make it a runtime error
      to leave a constructor without having initialized
      each non-nullable field.

   d) optionally, make it a compile-time error to exit a
      constructor without having initialized each 
      non-nullable object.

   // my preferred syntax:
   class Position {
      var x: Number;
      var y: Number;
      var target: Object!;

      function Position(x: Number, y: Number, target: Object!)
      {
         this.x = x;
         this.y = y;
         this.target = target;
      }
   }

If this discussion is already closed or if I am barking up the wrong
tree, please feel free to just say so. Thanks.

-Sho



More information about the Es4-discuss mailing list