Any news on multimethods?

Bruno Medeiros brunodomedeiros+spam at com.gmail
Wed Oct 18 04:12:51 PDT 2006


Jari-Matti Mäkelä wrote:
> Bruno Medeiros wrote:
>> Jari-Matti Mäkelä wrote:
>>> About 1,5 year ago there was short discussion about native multimethods
>>> in D on the NG. Well, is there any possibility to make them native on
>>> the language level. It's a bit annoying to use them via external
>>> libraries.
>> What's wrong with the library approach?
> 
> Well, nothing in particular. It's just a bit more illustrative to use a
> syntax like
> 
>   void method(foo @ bar) { .. }
> 

It is? I'm looking at that code segment and I have no ideia what it means.

> and support multimethods on the language level without extra 3rd party
> wrappers. Of course it adds more bloat in general, but I don't really
> know what D is trying to be now. It's too heavy to be a system
> programming language (out of the box) and it lacks many high level
> features that are needed in large scale software projects. Double
> dispatch is a very primitive feature and multimethods are one possible
> way to implement it.
> 
>> (and what multimethods library
>> are you using?)
> 
> I was testing the old hack by Pragma. There was a newer version of it
> somewhere but my newsreader was not able to open it.

I made a simple and experimental multimethods(multiple dispatch 
function) implementation not so long ago. It didn't work due to some DMD 
bugs (which have been fixed meanwhile) nor was it a complete 
implementation (I just wanted to see if and how it could be made), but 
the point is that it showed that a MDF implementation could be 
implemented library-side in a way almost (if not completely) as good as 
a in-language one.
For reference here is how the user can use a MDF:

   // Define the MDF dispatcher
   MultiDispatchFunction!(bool function(Shape, Shape)) checkCollisionMDF;
	
   // Add dispatches:
   checkCollisionMDF.AddDispatch(&checkCollisionCC);
   checkCollisionMDF.AddDispatch(&checkCollisionCR);
   checkCollisionMDF.AddDispatch(&checkCollisionRC);
   checkCollisionMDF.AddDispatch(&checkCollisionRR);

   // Call:
   checkCollisionMDF.call(new Circle, new Circle);
   checkCollisionMDF.call(new Rect, new Rect);
   checkCollisionMDF.call(new Circle, new Rect);
   checkCollisionMDF.call(new Rect, new Circle);

There is only one limitation: the MDF library has to have replicated 
code for each number of parameters of a MDF it wants to support, so the 
maximum number of parameters of the MDF function is arbitrarily limited.
But other than that, (correct me If I'm wrong) isn't that usage as 
simple as possible, in fact, as simple as CLOS's multimethods?


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list