dynamic classes and duck typing

Ary Borenszweig ary at esperanto.org.ar
Mon Dec 21 09:09:27 PST 2009


Ary Borenszweig wrote:
> Ary Borenszweig wrote:
>> retard wrote:
>>> Tue, 01 Dec 2009 03:16:47 -0800, Walter Bright wrote:
>>>
>>>> Ary Borenszweig wrote:
>>>>> Can you show examples of points 2, 3 and 4?
>>>> Have opDispatch look up the string in an associative array that returns
>>>> an associated delegate, then call the delegate.
>>>>
>>>> The dynamic part will be loading up the associative array at run time.
>>>
>>> This is not exactly what everyone of us expected. I'd like to have 
>>> something like
>>>
>>> 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.
>>
>> Exactly! That's the kind of example I was looking for, thanks.
> 
> Actuall, just the first part of the example:
> 
> void foo(Object o) {
>    o.duckMethod();
> }
> 
> Can't do that because even if the real instance of Object has an 
> opDispatch method, it'll give a compile-time error because Object does 
> not defines duckMethod.
> 
> That's why this is something useful in scripting languages (or ruby, 
> python, etc.): if the method is not defined at runtime it's an error 
> unless you define the magic function that catches all. Can't do that in 
> D because the lookup is done at runtime.
> 
> Basically:
> 
> Dynanic d = ...;
> d.something(1, 2, 3);
> 
> is just a shortcut for doing
> 
> d.opDispatch!("something")(1, 2, 3);
> 
> (and it's actually what the compiler does) but it's a standarized way of 
> doing that. What's the fun in that?

I take it back!

It would be very cool to have something like ruby's dynamic 
attribute-based finders in D:

http://api.rubyonrails.org/classes/ActiveRecord/Base.html



More information about the Digitalmars-d mailing list