This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Ruby until something better comes along
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
And no, with all due respect to my Perl loving comrades, it's not Perl 6. Sydney maybe. Or some OO-functional hybrid that has yet to be invented. But I digress.
This post is really about multiple dispatch. Or, as I call it - "method overloading". Yes, I've trademarked that term. Don't try and use it in your own blogs without paying me a quarter first. But I digress, again.
Piers mentions Perl subroutine attributes. As far as I can tell, the main use for subroutine attributes is for multiple dispatch, er, method overloading. Probably using a module like Attribute::Handlers.
In fact, that's about the only way I was able to understand subroutine attributes in Perl. I found the primary documentation on attributes to be strangely obtuse and unhelpful. I still find it a bass-ackwards way of handling dispatch. I mean, when would you use Attribute::Handlers over, say, Class::Multimethod? Maybe I'm missing something. But I digress yet again.
Anyway, here are two approaches with Ruby. The first is Nobu's overload package:
require 'overload'
def foo(arg)
...
end
overload(:foo, String)
def foo(arg1, arg2)
...
end
overload(:foo, String, String)
The second uses Ryan Pavlik's strongtyping package. require 'strongtyping' include StrongTyping
overload_error args end </pre> Then there's the strongtyping behavior from Sydney:
require 'strongtypingbehavior'
def foo(String x)
...
end
def foo(String x, String y)
...
end
Nice, eh? Yes, this is a bit of a rehash of a previous post, but I'm feeling less skittish about adding strongtyping behavior as part of the Sydney stdlib.