overload .
Mr.Bingo
Bingo at Namo.com
Mon Jun 25 15:39:09 UTC 2018
On Monday, 25 June 2018 at 13:58:54 UTC, aliak wrote:
> On Monday, 25 June 2018 at 13:37:01 UTC, Mr.Bingo wrote:
>>
>> One can overload assignment and dispatch so that something like
>>
>> A.x = ... is valid when x is not a typical member but gets
>> resolved by the above functions.
>>
>> Therefore, I can create a member for assignment. How can I
>> create a member for getting the value?
>>
>> A.x = 3; // Seems to get translated in to A.opDispatch!("x")(3)
>>
>> works but
>>
>> foo(A.x); // fails and the compiler says x does not exist
>>
>>
>> I need something consistent with opDot. I am trying to create
>> "virtual"(not as in function) fields and I can only get
>> assignment but not accessor.
>
> 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?
More information about the Digitalmars-d-learn
mailing list