dynamic classes and duck typing
Michel Fortin
michel.fortin at michelf.com
Sat Nov 28 05:34:50 PST 2009
On 2009-11-27 18:30:14 -0500, Walter Bright <newshound1 at digitalmars.com> said:
> But with a small feature, we can make this work:
>
> struct S
> {
> ...
> T opDynamic(s : string)(args...);
> }
>
> and then s.foo(3), if foo is not a compile time member of s, is rewritten as:
>
> s.opDynamic!("foo")(3);
>
> and opDynamic defers all the nuts-and-bolts of making this work out of
> the language and into the library.
Please make sure it can work to implement properties too.
The only thing that worries me is that "functions" defined through
opDynamic won't be reachable via reflection. There's no way to call
"foo" if "foo" is a runtime string; with regular functions you can use
compile-time reflection to build a dispatch table, but for those
implemented through opDynamic (which are not available through
reflection) it won't work.
Also, I would name it "opDispatch" instead. I fail to see anything
"dymamic" in it... it's a template so it's static isn't it? Of course
you can implement dynamic dispatch with this, but that's not a
requirement.
> In particular, opDynamic's parameter and return types should all be
> instances of std.variant.
That seems unnecessary. It's a template, so you should be able to
define opDynamic like this:
auto opDynamic(s : string, A...)(A args) { return args[0]; }
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list