constructor instead of opCall for an instance of a template alias

Rob T rob at ucora.com
Sat Nov 24 21:39:42 PST 2012


On Saturday, 24 November 2012 at 20:34:39 UTC, comco wrote:
> I have the following snippet:
>
> struct A(alias Method)
> {
>     string s;
>     this(Method method) {
>         method(s); // 5
>     }
> }
>
> struct B
> {
>     this(int i) { }
>     void opCall(string s) { }
> }
>
>
> void main() {
>     A!B(B(0));
> }
>
>
> This code fails to compile with the following errors:
> test.d(5): Error: constructor test.B.this (int i) is not 
> callable using argument types (string)
> test.d(5): Error: cannot implicitly convert expression (this.s) 
> of type string to int
> test.d(17): Error: template instance test.A!(B) error 
> instantiating
> shell returned 1
>
> It looks like the compiler is confusing the `method` instance 
> parameter with the `Method` class.
> If I replace line 5 with `method.opCall(s);` it compiles.
>
> Can you explain this behaviour please?


struct A(alias Method) is a template taking in some to be defined 
type named Method. I'm not sure why "alias" is there, you can 
remove it.

A!B(...) defines a struct A with B typed in as Method, so call to 
method(s); is the same as B(string) but there's no B.this(string) 
defined for B, causing the compiler to complain.

method.opCall(s); is B.opCall(s) which is defined, and that is 
why it will compile.

--rt




More information about the Digitalmars-d-learn mailing list