From: Thomas E Enebo Date: 2011-02-01T04:27:13+09:00 Subject: Re: why is overloading invalid in ruby. On Mon, Jan 31, 2011 at 9:00 AM, Gary Wright wrote: > > On Jan 31, 2011, at 5:16 AM, Shadowfirebird wrote: >> Or, indeed, the practical case - at the very heart of Ruby is the idea of duck typing.  Duck typing rules out method overloading, because parameters would have to have set types before you could have a signature.  Presumably no-one is suggesting that we should have fixed typing in Ruby? >> > > I think the original poster provided an example of overloading based on the number of parameters but not their type. > Even restricting yourself to overloading by arity is a bit problematic in Ruby because the arity still has to be determined (in some cases) dynamically: > > args = [1,2] > foo(*args)      # two arguments > args << 3 > foo(*args)      # three arguments Actually, arity of callsite is always calculated in Ruby to know if you should throw an ArgumentError (3 for 0 specified sort of errors) against the method you are calling. It seems like overloading based on arity is not such a bad idea to me based on some of the common arity parsing idioms people do by hand in the first few lines of their methods. What implementing arity-based overloads would do is get rid of most of this code we put at the top of methods and perform that logic in the Ruby implementation itself (in MRI in C vs in Ruby). Stylistically, I think the biggest issue is not realizing there are n overloads and then implementing less than n overloads in an overridden class. -Tom -- blog: http://blog.enebo.com       twitter: tom_enebo mail: tom.enebo@gmail.com