Q: Python 2.0 preliminary features?
Chris Double
chris at double.co.nz
Fri Oct 29 08:02:15 EDT 1999
skaller <skaller at maxtal.com.au> writes:
> I see. I had never thought about it this way, despite
> reading some theory based on this idea, you have put this
> very simply. Thanks!
Clos, Dylan and Cecil are examples of languages that have multiple
dispatch.
> That is, it would be possible to specialised
> the dispatcher for each message kind, depending
> on what makes sense for that kind of message.
With Dylan you can dispatch on what are called singleton objects. That
is, the exact identity of an object.
Contrived example:
// Default for all messages
define method handle-message( window, msg, wparam, lparam) ... end;
// Called when msg is the instance of the WM_CREATE object (which may
be an integer) on an instance of <edit-window>.
define method handle-message( window :: <edit-window>,
msg == WM_CREATE,
wparam,
lparam)
// Do something
...
// Call next most specific method
next-method();
end;
You probably get the idea. Clos has the same thing with 'EQL'
specialisers. Dylan can also dispatch on unions of types:
define method do-something( token :: one-of(#"word", #"sentence") )
end;
Where 'one-of' is a method which returns a type that behaves for type
matching purposes as a superclass of the types of the arguments passed
to it. The above method will be selected if called with
do-something(#"word") or do-something(#"sentence") but not anything
else.
I think Cecil is even more flexible in the things it can do in
dispatch. Clos is legendary for how flexible its dispatch mechanism
is.
Chris.
More information about the Python-list
mailing list