Experiment: Implementing Multiple Dispatch in D (need vararg call)
Bruno Medeiros
brunodomedeirosATgmail at SPAM.com
Wed Aug 2 14:33:10 PDT 2006
Bruno Medeiros wrote:
> I watched some time ago a Google TechTalk about Common Lisp (
> http://video.google.com/videoplay?docid=448441135356213813&q=peter+seibel
> ) where, as expected from a Lispanatic, LISP is idolized and Java is
> whined about.
>
> The second part of the talk, talks about the CLOS (Common Lisp Object
> System) multiple dispatch system (called multimethods on Lisp lingo),
> and how it can be useful, etc.., and how in Java it all had to be
> implemented manually and non-generically (i.e., for each particular usage).
>
Well, I think it is complete, although not fully working due to bug #274.
I've attached the code if anyone is interested. It is two files, the MDF
dispacher MultiDisFunction.d, some user/test code MDF_usercode.d, and
TomS's meta library (thx!). Basically the idea is this: suppose you have
two types of objects, Circle and Rect, and you want to check for a
collision between two objects of any of those types. The exact algorithm
will vary depending on the combination of the objects involved (checking
for collision between <Circle,Circle> is different from <Circle,Rect>),
so we create a multiple dispatch function that will redirect to each
algorithm depending on each parameter. (See the user code) This only
works for class parameters of course.
The dispatcher works by maintaining a map of entries, where the value is
a function pointer (a dispatch), and the key is an array of ClassInfo's,
which are the parameters of that dispatch. A dispatch is only selected
if the parameter's types match exactly, but this could be a more
complicated algorithm, checking superclass relations, etc., but that was
beyond my interest.
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MultiDisFunction.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20060802/a7027060/attachment.diff
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MDF_usercode.d
Url: http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20060802/a7027060/attachment-0001.diff
More information about the Digitalmars-d-learn
mailing list