overload .

aliak something at something.com
Mon Jun 25 18:37:50 UTC 2018


On Monday, 25 June 2018 at 15:39:09 UTC, Mr.Bingo wrote:
> On Monday, 25 June 2018 at 13:58:54 UTC, aliak wrote:
>> A.x is translated in to A.opDispatch!"x" with no args. So I 
>> guess you can overload or you can static if on a template 
>> parameter sequence:
>>
>> import std.stdio;
>> struct S {
>>     auto opDispatch(string name, Args...)(Args args) {
>>         static if (!Args.length) {
>>             return 3;
>>         } else {
>>             // set something
>>         }
>>     }
>> }
>> void main()
>> {
>>     S s;
>>     s.x = 3;
>>     writeln(s.x);
>> }
>>
>> Cheers,
>> - Ali
>
> Ok, for some reason using two different templated failed but 
> combining them in to one passes:
>
> auto opDispatch(string name, T)(T a)
> auto opDispatch(string name)()
>
> Maybe it is a bug in the compiler that it only checks one 
> opDispatch?

Two opDispatchs as in:

import std.stdio: writeln;
struct S {
     void opDispatch(string name, T)(T t) {
         writeln(t);
     }

     auto opDispatch(string name)() {
         writeln("ret");
         return 4;
     }
}

void main() {
     S s;
     s.x;
     s.x = 4;
}

??

The above seems to work fine. Or maybe you meant something else?



More information about the Digitalmars-d-learn mailing list