www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Newb's questions constructor

Reading 'the book' and it states that D does not allow one to 
nest construction calls that are not "complete".


    if (x > 1) { this(x); }

will fail.

First, I do not know why this is required. It seems to me it 
would be better to allow one optionally use another constructor 
if it is desired rather than forcing all paths to use it.

Also, It is easy to avoid this by alaising "this(...)"(using 
member functions that this(...) wraps). This, then is exactly the 
same semantically above but avoids the error:

    if (x > 1) { thiswrapper(x); }

this(double x) { thiswrapper(x); }

(so, the constraint in D only increases code bloat as far as I 
can tell and, hence, unnecessary)

Second, steaming from this issue with constructors, how can we 
get the address of a constructor, or alternative, store it in a 

class Test
    static void function(Test, double) thiswrapper;

       thiswrapper = ????this(double);
       if (x > 1) { thiswrapper(x); }

    this(double x) { }

The point of the example code(only an example, I know it is 
rather useless but so are babies, right?) is to simply do the 
same the previous example but sort of backwards. The previous 
example has thiswrapper being wrapped by this(double) while in 
this case thiswrapper wraps this(double). They will will have 
virtually identical semantics with different syntax.

The second case allows us to solve the original problem a bit 
more logical. (in fact, if we had some type of "multi-function 
pointer" that allowed overloads we could, say, assign all the 
constructor functions to thiswrapper and let the compiler choose 
the appropriate one)

In any case the second question involves assigning functions to 
function pointers/delegates, specifically the constructor 

I guess it would be the a similar problem if I wanted to assign a 
function pointer to a member of a class from a pointer to that 

class Test { void mymethod(...){} void mymethod(....){}}

void function(...) mymethodptr = t->mymethod; (which should be 
the same as Test.mymethod?)

Nov 29 2012