www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to write a class proxy ?

reply boolangery <eliott.dumeix gmail.com> writes:
Hi,

What is the best way to write a class proxy ?
I need to intercept attribute access.

Example:

The user write a PODO:

class User
{
     string name;
}


Then he use my library:

auto user = mylib.get!User(); // return a proxy here

writeln(user.name); // intercept user.name access (how to do this 
?)
Mar 20
parent Alex <sascha.orlov gmail.com> writes:
On Wednesday, 20 March 2019 at 08:16:22 UTC, boolangery wrote:
 Hi,

 What is the best way to write a class proxy ?
 I need to intercept attribute access.

 Example:

 The user write a PODO:

 class User
 {
     string name;
 }


 Then he use my library:

 auto user = mylib.get!User(); // return a proxy here

 writeln(user.name); // intercept user.name access (how to do 
 this ?)
This is by far not unique and depends on features of the proxy, even if you restrict the user to PODOs... Besides this, for example: ´´´ module mylib; auto get(T)() { return new Proxy!T(); } private class Proxy(T) { T podo; alias podo this; this() { podo = new T(); } } ´´´
Mar 20