dynamic classes and duck typing

Leandro Lucarella llucax at gmail.com
Sun Nov 29 14:48:03 PST 2009


retard, el 29 de noviembre a las 18:27 me escribiste:
> Sun, 29 Nov 2009 14:59:27 -0300, Leandro Lucarella wrote:
> 
> > Walter Bright, el 27 de noviembre a las 15:30 me escribiste:
> >> 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:
> > 
> > I like the feature, but I don't understand where is the duck-typing in
> > all this. I think you're confusing duck-typing with dynamic-typing or
> > I'm missing something?
> 
> Well it seems like the duck typing happens all on compile time with the 
> new feature. You get some of the features of true dynamic languages, but 
> not all. You can't really write python/ruby style dynamic code with it, 
> e.g.
> 
> class foo {
>   void sayHello() { print("hello"); }
> }
> 
> auto bar = new foo();
> 
> try {
>   bar.sayBye();
> }
> catch(MethodNotFoundException e) {
>   ...
> }
> 
> auto bye_routine(Object o) {
>   return o.sayBye();
> }
> 
> bar.sayBye = { bar.sayHello(); return "and bye"; }

I guess this is a proposed syntax or something right? I guess you're
omitting the opDispatch() implementation on purpose. Is property syntax
really allowed to assign a new method?

> println(bye_routine(bar));
> 
> Of course this is inefficient and error prone but that's what's it all 
> about in dynamic languages. You get tons of flexibility.

I see. As I said in the reply to Walter, I think we need more support if
we really want to make dynamic typing (and duck-typing) pleasant in D. As
I said, there should be a better way to ask if an object have some methond
than trying to use it and catch an exception (like Python's hasattr()). It
would be very nice to be able to add methods (and properties!) dynamically
to an object too, this is very common in dynamic languages.

I know all this can be done, but I think we need an standard facility to
avoid everybody implementing its own dynamic typing "framework", which
would be a mess to use and hard to interoperate between different
implementations. It doesn't have to be a language feature though, if it
can be implemented in Phobos.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Una mujer en bicicleta, con sombrero de paja, es la más flagrante
violación a las leyes de la aerodinamia.
	-- Ricardo Vaporeso. 21 de Septiembre de 1917.



More information about the Digitalmars-d mailing list