dynamic classes and duck typing
Max Samukha
spambox at d-coding.com
Tue Dec 1 05:14:56 PST 2009
On Tue, 1 Dec 2009 11:45:14 +0000 (UTC), retard <re at tard.com.invalid>
wrote:
>>>
>>> void foo(Object o) {
>>> o.duckMethod();
>>> }
>>>
>>> foo(new Object() { void duckMethod() {} });
>>>
>>> The feature isn't very dynamic since the dispatch rules are defined
>>> statically. The only thing you can do is rewire the associative array
>>> when forwarding statically precalculated dispatching.
>>
>> I believe you should distinguish duck types from other types.
>>
>> You shouldn't be able to call duckMethod given a reference to Object,
>> it's a statically-typed language, after all.
>
>Agreed. But this new feature is a bit confusing - there isn't anything
>dynamic in it. It's more or less a compile time rewrite rule. It becomes
>dynamic when all of that can be done on runtime and there are no
>templates involved.
But the feature can be used to implement fully dynamic behavior
(provided there is extended RTTI, which is already implementable on
top of compiletime introspection using a tweaked compiler). For
example, Variant can implement opDispatch to forward calls to the
contained object:
void foo(Variant o) {
o.duckMethod();
}
foo(Variant(new class { void duckMethod() {} }));
BTW, it is not possible currently to create a Variant from a void
pointer to the object and the meta-object of that object because D's
meta-objects are lacking necessary information. But that is fixable, I
guess.
More information about the Digitalmars-d
mailing list