Multiple dispatch (Re: [squeak-dev] The Inbox: Kernel-cmm.464.mcz)

Frank Shearar frank.shearar at angband.za.org
Sun Jun 20 18:35:52 UTC 2010


On 2010/06/20 16:38, Michael Haupt wrote:
> Hi,
>
> On Sun, Jun 20, 2010 at 4:11 PM, Frank Shearar
> <frank.shearar at angband.za.org>  wrote:
>>> Since Smalltalk does not support multi-methods, it is essential that the
>>> caller of a method can pass arguments in multiple formats to the same
>>> method and have them correctly interpreted by the implementation.
>> ...
>> (I assume you are talking about multiple dispatch - method lookup using not
>> just the receiver but also the types of the parameters to a selector, like
>> Common Lisp's generic functions?)
>
> looks more like varargs to me ...

I think you're right, Michael.

Objective-C's varargs look like this:

   NSString *s = [NSString stringWithFormat: @"a string %@, a float: 
%1.2f", @"string", 3.1415];

Note the comma separated values there, in contrast to the more usual 
keyword syntax like Smalltalk:

   [myObj aSelector: @"hello" withMultiple: 1 args: 2];

The varargs function exactly like &rest parameters in Common Lisp - a 
list of values. So it sounds like Brent and Chris want those format 
strings (or other vararg-using selectors) to behave the same way with a 
single object as with a collection, where the single object is treated 
as a single-element collection.

On the implementation side of such a "varargs" method, you'd have

   varargSelector: aCollectionOrObject
     ^ (aCollectionOrObject asArray) inject: 0 into: [:total :n | total 
:= total + n].

instead of the rather uglier

   varargSelector: aCollectionOrObject
     | coll |
     coll := aCollectionOrObject isCollection
       ifFalse: [Array with: aCollectionOrObject].

     ^ (coll asArray) inject: 0 into: [:total :n | total := total + n].

In a library that has many vararg-like selectors, this can make a nice 
improvement.

frank



More information about the Squeak-dev mailing list