Dynamic D

Radu rr at foo.bar
Tue Jan 4 02:12:43 PST 2011


On 1/4/2011 12:23 AM, Adam Ruppe wrote:
> Over the weekend, I attacked opDispatch again and found some old
> Variant bugs were killed. I talked about that in the Who uses D
> thread.
>
> Today, I couldn't resist revisiting a dynamic kind of object, and
> made some decent progress on it.
>
> http://arsdnet.net/dcode/dynamic.d
>
> (You can compile that; there's a main() at the bottom of that file)
>
> It isn't quite done - still needs op overloading, and probably better
> errors, but it basically works.
>
> It works sort of like a Javascript object.
>
> Features:
>
> opDispatch and assignment functions:
> Dynamic obj;
>
> // assign from various types
> obj = 10;
> obj = "string";
>
> obj.a = 10; // assign properties from simple types naturally
>
> // can set complex types with one compromise: the () after the
> // property tells it you want opAssign instead of property opDispatch
> obj.a() = { writefln("hello, world"); }
>
> // part two of the compromise - to call it with zero args, use call:
> obj.a.call();
>
> // delegte with arguments works too
> obj.a() = delegate void(string a) { writeln(a); };
>
> // Calling with arguments works normally
> obj.a("some arguments", 30);
>
>
> Those are just the basics. What about calling a D function? You
> need to convert them back to regular types:
>
> string mystring = obj.a.as!string; // as forwards to Variant.coerce
>      // to emulate weak typing
>
> Basic types are great, but what about more advanced types? So far,
> I've implemented interfaces:
>
> interface Cool {
>      void a();
> }
>
> void takesACool(Cool thing) { thing.a(); }
>
>
>
> takesACool(obj.as!Cool); // it creates a temporary class implementing
> // the interface by forwarding all its methods to the dynamic obj
>
>
>
> I can make it work with structs too but haven't written that yet.
> I want to add some kind of Javascript like prototype inheritance too.
>
>
>
> I just thought it was getting kinda cool so I'd share it :)

To cool!
I remember trying to do the same for D1 for my Hessian lib, but you hit 
the spot here.

This is a great little start for easy RPC proxy creation and for 
interoperability with COM and others.

Great work!


More information about the Digitalmars-d mailing list