dynamic classes and duck typing
Walter Bright
newshound1 at digitalmars.com
Fri Nov 27 15:30:14 PST 2009
One thing Java and Python, Ruby, etc., still hold over D is dynamic
classes, i.e. classes that are only known at runtime, not compile time.
In D, this:
s.foo(3);
could be emulated with:
s.dynamicMethod("foo", 3);
Unfortunately, that makes it impossible to use s with generic code
(besides looking unappealing). 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.
In particular, opDynamic's parameter and return types should all be
instances of std.variant.
(This has come up in various forms in this n.g. before, but I don't have
any references handy.)
More information about the Digitalmars-d
mailing list