Feature request - simpler constructors
Regan Heath
regan at netmail.co.nz
Thu Sep 20 08:55:40 PDT 2007
Alternate syntax suggestion:
class Point {
int x;
int y;
int foo; //not implicitly assigned any value
this(int x, int y, int bar):(x,y) //bar is not assigned to anything
{
//super constructor calls go here
//members are assigned here (_after_ calling super)
//first line of user code goes here
if (x > 5) {} //refers to member 'x' not parameter 'x'
}
}
The idea(s) here being:
1. If you want to implicitly assign parameters to members you must give
the parameters 'exactly' the same name as the member. (I find myself
wanting to do this anyway)
2. If you use the member/parameter name inside the constructor you will
be referring to the member, and not the paramter. (Essentially the
parameter ceases to exist the moment it has been assigned to the member)
3. The variables to be assigned are specified by name (only) in the :()
part of the constructor. (The compiler will verify these
members/parameters exist - requires verifying members in base classes too)
4. Assignments happen after calls to super* but before the first line of
constructor code. This ensures they 'overwrite' any values assigned in
super calls. In the case of an explicit super call assignments happen
before the first line of constructor code, before the explicit super call.
This syntax gives a bit more flexibility that the original idea at the
cost of some verbosity but I think it's still fairly clean and clear.
Regan
More information about the Digitalmars-d
mailing list