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

Adam D. Ruppe destructionator at gmail.com
Sat Jan 4 11:52:11 PST 2014


On Saturday, 4 January 2014 at 19:26:50 UTC, Gary Willoughby 
wrote:
> Got it!

opDispatch rox, and there's all kinds of crazy stuff you can do 
with it. In my dom.d, I used it for three things:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/dom.d

1) easy access to attributes (node.href = "dlang.org"; and auto 
href = node.href;) using the technique you just showed.

2) the style stuff, which does a little rewriting:

node.style.marginLeft = "10px"; -> adds "margin-left: 10px;" to 
the style attribute

3) Forwarding collections:

document[".cool"].addClass("cool2");

uses opDispatch to forward any method to the collection without 
me writing much boilerplate code.


This is also nice for calling external dynamic apis, you can use 
opDispatch to forward over a network call or something like that.



I also used it in my jsvar.d file to make a var type in D, very 
similar to in Javascript:

https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/jsvar.d

var a = var.emptyObject;
a.cool = { writeln("hello world!"); }
a.cool()(); // double parens needed due to broken @property

a.bar = 10;
writeln(a.bar); // works

and so on. D rox so much.


More information about the Digitalmars-d-learn mailing list