constructor instead of opCall for an instance of a template alias

Maxim Fomin maxim at maxim-fomin.ru
Sun Nov 25 08:42:02 PST 2012


On Sunday, 25 November 2012 at 15:27:53 UTC, comco wrote:
>
>> 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:

Welcome to D structs creation puzzle.

> skipped

If you change return type from void to e.x. int, dmd will still 
complain "Error: need 'this' to access member opCall".

There are two problems here: firstly, by some reason dmd does not 
distinguish between static and non static methods in a sense that 
it allows to call static method on instance and takes into 
account non-static methods with expression operating on types 
(like this case).

Secondly, and again by some reason three "semantic things" such 
as struct literals, struct constructors and opCalls use same 
syntax like "S()" (let alone it may be non-member function). This 
makes their priority order an issue - for 2.060 version it is: 
1)opCall 2)ctor 3)literal. This is why dmd complains about 
"A.opCall (int i) is not callable using argument types ()": it 
recognizes an opCall method, does not take into account 
staticness and tries to convert A() into opCall invocation.

Recently I saw a major pull affecting this behavior, so in 2.061 
the situation may be changed (I haven't bother to figure yet). In 
practice this makes a tricky thing to understand what S() is and 
creates a problem when you e.x. heavily use struct ctor, write 
opCall method and everything breaks due to dmd tries now to call 
opCall instead of ctor.



More information about the Digitalmars-d-learn mailing list