Clint
3/21/2006 6:04:00 AM
Trans wrote:
> Thanks Andrew.
>
> I'm afraid I don't care for how the strongtyping lib handles
> overloading with the #overload call internal to the method. It
> undermines dynamic capabilities. Eg.
>
> def bar(*args)
> overload(args, String, String) {
> | s1, s2 |
> ...
> return
> }
>
> overload(args, String, Integer) {
> | s, i |
> ...
> return
> }
>
> overload_error args
> end
>
> It would be tricky to add another overload post defnition of the
> original.
I would agree. You loose the most powerful part - to define methods after the
definition. One good example for multimethods are in event callbacks. You can
capture the event you want without a bunch of switch code:
overload :mouse_down, LeftButton, ButtonDown do |button, state, x, y|
puts "left mouse button down"
end
Unfortunately it looks a bit odd. If I was using it in a library internally it
might be cool. But exposing library users to it would be something else. I
wonder if it could be done like this:
class Test
generic :foo
def foo.Integer_String( x, y )
return x * y
end
end
t = Test.new
t.foo( 10, 20 )
Here's a prototype, but doesn't quite work:
def generic( symbol )
class_eval do
#instance_variable_set( "@@#{symbol.to_s}", Object.new )
@@foo = Object.new
define_method( symbol ) do | *args |
# Find applicable method and call
@@foo.Integer_String( *args )
end
end
end
Mike