Consistency, Templates, Constructors, and D3

F i L witte2008 at gmail.com
Fri Aug 24 17:35:35 PDT 2012


Nathan M. Swan wrote:
> The constructor definition syntax doesn't seem to be an 
> improvement: this new instead of the old this.

well the reason they're named is because then you can multiple 
constructors under different names:

     class Model
     {
         string name;
         float x = 0, y = 0;

         this new(string n) {
             name = n;
         }

         this new(string n, float x, float y) {
             name = n;
             this.x = x;
             this.y = y;
         }

         this load(string fn) {
             auto file = File.load(fn);
             ...
         }
     }

Here we have two overloads of the constructor new() without 
conflict, but also two constructors that would have conflicted if 
they weren't separated by name: new(string) and load(string).

In this situation today, we would normally need to make a static 
factory function called 'load()' which created a 'new Model()' 
and returned it. We do this factory function thing all the time 
today, and it's required for things like Memory Pools and to 
resolve naming conflicts like above. Ideally, there should be a 
single, consistent way of creating objects which allow for 
arbitrary named separation, and I think this is best solution.

Both Vala and Dart have a named-constructor syntax to address 
this issue, but neither feels as consistent (to me) as what I'm 
presenting above.



More information about the Digitalmars-d mailing list