On 1/6/11 1:22 AM, Robert Jacques wrote:
On Mon, 03 Jan 2011 17:23:29 -0500, Adam Ruppe
<destructiona...@gmail.com> 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.
[snip]
I've been working on an update to both std.json and std.variant.
Previews of both are available here:
https://jshare.johnshopkins.edu/rjacque2/public_html/
though they are still works in progress. Two of the big enhancements
that you might be interested in are call support and opDispatch +
reflection + prototype structs. To paraphrase your example:
Variant v;
v.a( 10 );
assert(v.a == 10);
v.a( { writefln("hello, world"); } );
v.a.call; //To be replaced by opCall, once struct opCall is fixed (Bug
4053)
v.a( delegate void(string a, int x) { foreach(i;0..x) writeln(i+1,"
",a); } );
v.a("potatoes", 3);
I've also stubbed out a prototype style object, but I haven't really
tested it yet. Thoughts, comments and use/test cases are always welcomed.
I think this transgresses the charter of Variant. Variant is meant to
hold an object of some _preexisting_ type, not to morph into anything.
We should have three abstractions:
* Algebraic holds any of a closed set of types. It should define method
calls like a.fun(args) if and only if all of its possible types support
the call with compatible arguments and return types.
* Variant holds any of an unbounded set of types. Reflection may allow
us to define v.fun(args) to look up the method name dynamically and
issue a runtime error if it doesn't exist (sort of what happens now with
operators).
* Dynamic is a malleable type that you get to add state and methods to,
just like in Javascript.
Andrei