constructor instead of opCall for an instance of a template alias

comco void.unsigned at gmail.com
Sun Nov 25 07:27:52 PST 2012


> A!B(...) defines a struct A with B typed in as Method, so call 
> to method(s); is the same as B(string)
Why is that? Here method is an instance of the type Method, not 
the type Method itself, so by saying `method(s)` we can't mean a 
constructor. Something very strange happens...
I have a simpler example now:

import std.stdio;

enum ok = true;

struct A
{
     int i;

     static if (ok)
     {
         this(int i) {
             this.i = i;
             writeln("A.ctor();");
         }
     }

     void opCall(int i) {
         writeln("A.opCall()");
     }
}


void main() {
     static if (ok)
     {
         A(0);
     }
     else
     {
         A();
     }
}

This example works as expected, and it prints: A.ctor().
But if I change it to enum ok = false, then the above code 
doesn't compile with an Error: function LowestAncestor.A.opCall 
(int i) is not callable using argument types (). So this might 
mean that the compiler becomes confused somehow and thinks that 
we want to call opCall on a static instance (which is not even 
possible, because opCall is an instance method), and not the 
default constructor.


More information about the Digitalmars-d-learn mailing list