Is it possible to handle 'magic' property assignments a'la PHP?

Gary Willoughby dev at nomad.so
Sat Jan 4 11:17:30 PST 2014


On Saturday, 4 January 2014 at 19:08:45 UTC, Gary Willoughby 
wrote:
> In D is it possible to handle accessing class properties that 
> don't exist, similar to PHP's magic methods?
>
> http://www.php.net/manual/en/language.oop5.overloading.php#object.set
>
> For example, the below class doesn't have the 'x' property:
>
> class T
> {
>     this()
>     {
>         this.x = "hello world!";
>     }
> }
>
> Can i handle this in a programmatic way to intercept the 
> property and deal with it?
>
> Something like this:
>
> class T
> {
>     this()
>     {
>         this.x = "hello world!";
>     }
>
>     opMagic(A, B)(A property, B value)
>     {
>         // property = "x", value = "hello world!"
>     }
> }

OMG i've just found opDispatch!

http://dlang.org/operatoroverloading.html#Dispatch

Fantastic!


More information about the Digitalmars-d-learn mailing list