From: Philipp Meier Date: 2002-09-12T22:11:19+09:00 Subject: Re: not grasping the method overloading/multi-dispatch thing --PEIAKu/WMn1b1Hv9 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Sep 12, 2002 at 09:53:31PM +0900, Hal E. Fulton wrote: > From: > Sent: Thursday, September 12, 2002 7:14 AM > > On Thu, 12 Sep 2002, Friedrich Dominicus wrote: > > > dblack@candle.superlink.net writes: > > > As Matz pointed out overloading would be in the spirit of Ruby > >=20 > > Right, but that's the part I don't get :-) I'm picturing things like: > >=20 > > def meth(Integer n, String s) > > ... > > def meth(Float f, String s) > > ... > >=20 > > and somewhere along the line I'm not clear on how that fits into the > > spirit/design/philosophy of Ruby. >=20 > My opinion: It just doesn't. > To declare variables at all is not Rubyish IMO > (especially when assigning types, which can be > a separate issue). > To create a method signature by assigning types > to parameters is even worse, as far as I can see. > It looks like a huge step backward toward Java > and C++. As long as not declaring an argument type would mean "Object" (or any type) you wouldn't loose anything. Currently you can use self-made overlaoding methods like that (untested): class Foo def self.overload(symbol, args, proc) @@methods << [:symbol, args, proc] end def call_overload(symb, *args)=20 types =3D args.map{|a| a.type} method =3D @@methods.find { |a| a[0] =3D=3D symb && a[1] =3D=3D types } raise ArgumentError, "Unable to find overloaded method for #{symb}, #{types.inspect}" unless method method.call(*args) end overload :meth, [Integer, Integer], Prow.new { |a,b| a.to_s + b.to_s }=20 overload :meth, [Object, Object], Prow.new { |a,b| a + b }=20 def meth(arg1, arg2) do_overload(:meth, arg1,arg2) end end Foo.new.meth("s", "b") # =3D> sb Foo.new.meth("s", 1) # =3D> s1 Foo.new.meth(2, 1) # =3D> 21 The same could be implemented with the folling hypothetical syntax: class Foo def meth(Integer i, Integer j)=20 i.to_s + j.to_s end def meth(Object o1, Object o2)=20 o1+o2 end end Which is reasonably shorted and easier to understand. I see a need for this as I find myself often manually dispatching to methods by the type of an argument. -billy. --=20 Meisterbohne S=F6flinger Stra=DFe 100 Tel: +49-731-399 499-0 eL=F6sungen 89077 Ulm Fax: +49-731-399 499-9 --PEIAKu/WMn1b1Hv9 Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux) iD8DBQE9gJKWfBriNoqItSYRAiWbAJsELwNNXYlvW70I1GVlRmanei6OTwCfXLGV 9zKskE8hA3POSZVMeSgxYs8= =7MQc -----END PGP SIGNATURE----- --PEIAKu/WMn1b1Hv9--