From: Dan Sugalski Date: 2002-09-11T23:56:39+09:00 Subject: Multimethods (was Re: Larry Wall's comments on Ruby) At 10:48 PM +0900 9/11/02, Gavin Sinclair wrote: >Despite the discussion and links, I haven't picked up on what multimethods >actually are, so I can't comment on those. Multimethod dispatch is reasonably simple. It's when you have multiple methods with the same name, which are only differentiated by their method signature. At runtime, the types of the arguments are checked and you dispatch to the matching method. If no method matches, or more than one method matches equally well, an exception is thrown. This is a useful thing to do, as it means you can avoid casting or converting. For example, if you have: a = b + c b and c are both integers, you want to return an integer. If one is an integer and the other a float, you want to return a float but do integer math (as its faster to add an int and a float than it is to add two floats), and if they're both floats you want to do a float add. (And if they're extended precision numbers you might want to go to even more lengths, as it's usually much cheaper to add a platform int to an extended precision number than coerce an int to an extended precision number and do the add) That's pretty much multimethods. They're terribly useful, as they cut out a lot of checks for the second type in methods. (Checks which tend to be slow, error prone, and missing stuff) -- Dan --------------------------------------"it's like this"------------------- Dan Sugalski even samurai dan@sidhe.org have teddy bears and even teddy bears get drunk