From: Paul Brannan Date: 2002-09-12T23:30:49+09:00 Subject: Re: Multimethods (was Re: Larry Wall's comments on Ruby) On Thu, Sep 12, 2002 at 02:59:22PM +0900, Mike Lucius wrote: > Multimethod dispatch (which is like C++ function overloading, but not > quite)is when there are two(or more) receivers. It is exactly like > C++ function overloading except the method would presumably have > access to the internal state of both(all) instances dispatched upon. With C++ function overloading the function to call is picked at compile-time. Wouldn't the multimethod to call be picked at run-time (like virtual functions)? > i.e. it has the status of "method" for both classes. C++ doesn't > support this idea directly, but Alexandrescu uses Template Programming > to make a fair approximation (using double-dispatch IIRC). > > Function overloading does the same thing, but functions are not > methods and this limits their usefulness. What is the difference between a virtual member function and a method? > The executive summary for function overloading: > For a particular function name different combinations of > argument types are mapped to different implementations. > > Generic programming is when an algorithm (function) is defined, but > types are not specified (completely). Types are bound at compile time > (C++ Haskell OCAML) or, in the case of dynamic typing, types are > checked by the programmer. > > Ruby does generic programming naturally because types aren't checked. > > One problem that crops up here is syntax may be confused with > semantics (e.g. the + operator doesn't commute with strings). I've > noticed that Mix-ins are used to guarantee the semantics of operators > in some cases. > > The executive summary for generic programming: > For a particular function name different combinations of > argument types are mapped to the same implementation. > > > In C++ generic functions are implemented by the compiler generating > overloaded functions as required. IMO it is very ugly These overloaded functions are generated by templates. Having a different function generated at compile-time for every combination of parameters may be ugly, but does offer speed. You can get a similar effect as Ruby if everything inherits from a common base class and you add a little bit of extra type information to your classes (XTTI perhaps?). > Some functional languages, like Haskell, combine the two ideas by > allowing functions to be implemented in serveral different ways, but > the types of the arguments are specified by patterns so that many > different type "signatures" get mapped to one implementation. > > Mike Lucius Paul