www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - overload .

reply Mr.Bingo <Bingo Namo.com> writes:
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.
Jun 25 2018
parent reply aliak <something something.com> writes:
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
Jun 25 2018
parent reply Mr.Bingo <Bingo Namo.com> writes:
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?
Jun 25 2018
parent aliak <something something.com> writes:
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?
Jun 25 2018