November 29, 2009
Lutger wrote:
> Walter Bright wrote:
> 
>> And here it is (called opDispatch, Michel Fortin's suggestion):
>>
>>
> http://www.dsource.org/projects/dmd/changeset?new=trunk%2Fsrc@268&old=trunk%2Fsrc@267
> 
> holy duck, that is quick!

Unfortunately, things turned out to be not quite so simple. Stay tuned.
November 29, 2009
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?
> 

Perhaps I am using the term wrong, but I figure it's duck-typing if you can go ahead and try to access methods of an object, and they are checked at runtime and throw some kind of method-not-found exception if they aren't there.

With this, it should be possible to construct a type at runtime, and have it work with statically compiled code. This should work very nicely to implement a plug in architecture.
November 29, 2009
Walter Bright, el 29 de noviembre a las 13:52 me escribiste:
> 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?
> 
> Perhaps I am using the term wrong, but I figure it's duck-typing if you can go ahead and try to access methods of an object, and they are checked at runtime and throw some kind of method-not-found exception if they aren't there.

OK, now I see what you mean. Perhaps it would be helpful to have
a standard exception for unexistent methods to support that idiom. If we
don't every library will create one and it would be a mess.

A standard way to test if a method exists would be nice too, something
like Python's getattr(), setattr() and hasattr() can be a start.

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
Demasiado lento para una estrella fugaz
Demasiado limpio para lo que vos acostumbras
Demasiado claro para tanta oscuridad
Demasiados sueños, poca realidad
November 29, 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.
November 30, 2009
Walter Bright wrote:
> And here it is (called opDispatch, Michel Fortin's suggestion):
> 
> http://www.dsource.org/projects/dmd/changeset?new=trunk%2Fsrc@268&old=trunk%2Fsrc@267 
> 

Fixed reported problems with it:

http://www.dsource.org/projects/dmd/changeset?old_path=trunk&old=269&new_path=trunk&new=270
November 30, 2009
On Mon, 30 Nov 2009 07:10:41 +0100, Walter Bright <newshound1@digitalmars.com> wrote:

> Walter Bright wrote:
>> And here it is (called opDispatch, Michel Fortin's suggestion):
>>  http://www.dsource.org/projects/dmd/changeset?new=trunk%2Fsrc@268&old=trunk%2Fsrc@267
>
> Fixed reported problems with it:
>
> http://www.dsource.org/projects/dmd/changeset?old_path=trunk&old=269&new_path=trunk&new=270

This still does not compile:

struct foo {
	void opDispatch( string name, T )( T value ) {
	}
}

void main( ) {
	foo f;
	f.bar( 3.14 );
}

test.d(10): Error: template instance opDispatch!("bar") does not match template
declaration opDispatch(string name,T)

-- 
Simen
November 30, 2009
Simen kjaeraas:
> test.d(10): Error: template instance opDispatch!("bar") does not match template declaration opDispatch(string name,T)

For Walter: this quick coding-testing cycle is an huge improvement over the original way DMD was developed. Things can be improved still of course, but this was an important step.

Bye,
bearophile
November 30, 2009
Simen kjaeraas wrote:
> This still does not compile:
> 
> struct foo {
>     void opDispatch( string name, T )( T value ) {
>     }
> }
> 
> void main( ) {
>     foo f;
>     f.bar( 3.14 );
> }
> 
> test.d(10): Error: template instance opDispatch!("bar") does not match template
> declaration opDispatch(string name,T)

It works when I try it.
November 30, 2009
Walter Bright Wrote:

> Simen kjaeraas wrote:
> > This still does not compile:
> > 
> > struct foo {
> >     void opDispatch( string name, T )( T value ) {
> >     }
> > }
> > 
> > void main( ) {
> >     foo f;
> >     f.bar( 3.14 );
> > }
> > 
> > test.d(10): Error: template instance opDispatch!("bar") does not match
> > template
> > declaration opDispatch(string name,T)
> 
> It works when I try it.

It does. Shouldn't this work also?

struct foo {
    void opDispatch( string name, T... )( T values ) {
    }
}

void main( ) {
    foo f;
    f.bar( 3.14 );
}

Álvaro Castro-Castilla
November 30, 2009
Álvaro Castro-Castilla wrote:
> It does. Shouldn't this work also?
> 
> struct foo {
>     void opDispatch( string name, T... )( T values ) {     }   }
>                                                                                                                                        void main( ) {     foo f;
>     f.bar( 3.14 );
> }

Declare as:

    void opDispatch(string name, T...)(T values...)
                                               ^^^