Duby dynamic dispatch has landed!

Rémi Forax forax at univ-mlv.fr
Thu Mar 25 08:12:20 PDT 2010


Le 25/03/2010 03:17, Charles Oliver Nutter a écrit :
> I love Duby sometimes, I really do.
>
> I've just landed full dynamic dispatch support. Here's an example
> script and session:
>
> ~/projects/duby ➔ cat examples/dynamic.duby
> # Example of using a dynamic type in a method definition
> def foo(a:dynamic)
>    puts "I got a #{a.getClass.getName} of size #{a.size}"
> end
>
> class SizeThing
>    def initialize(size:int)
>      @size = size
>    end
>
>    def size
>      @size
>    end
> end
>
> foo([1,2,3])
> foo(SizeThing.new(12))
>
> ~/projects/duby ➔ bin/duby examples/dynamic.duby
> I got a java.util.Collections$UnmodifiableRandomAccessList of size 3
> I got a examples.SizeThing of size 12
>
> I like this example because it's two totally disjoint types that
> happen to both have a "size" method, and Duby + indy + dynalang
> handles them just fine.
>
> So far I'm doing it like C#, specifying a "dynamic" type that
> propagates as-is through the type inference phase(s), but for bytecode
> and signature generation it gets erased to java.lang.Object.
>    

I've also found useful to propagate expected type back to the calling 
method.
Example:
if (foo()) { ... }

should be translated to indy foo ()Z instead of ()Object because if () 
wait for a boolean.

> - Charlie
>    

Rémi


More information about the mlvm-dev mailing list