Consistency, Templates, Constructors, and D3

Nathan M. Swan nathanmswan at gmail.com
Fri Aug 24 10:00:12 PDT 2012


On Friday, 24 August 2012 at 05:14:39 UTC, F i L wrote:
> We replace it with special factory functions. Example:
>
>     class Person {
>       string name;
>       uint age;
>
>       this new(string n, uint a) {
>         name = n;
>         age = a;
>       }
>     }
>
>     void main() {
>       auto philip = Person.new("Philip", 24);
>     }
>
> Notice 'new()' returns type 'this', which makes it static and 
> implicitly calls allocation methods (which could be overridden) 
> and has a 'this' reference.
>

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

The constructor calling syntax is actually something I've thought 
of before. I think Class.new is better than new Class, simply 
because it's more succinct when chaining:

Class.new().method() vs. (new Class()).method()

NMS


More information about the Digitalmars-d mailing list