Experiment: Implementing Multiple Dispatch in D (need vararg call)
Bruno Medeiros
brunodomedeirosATgmail at SPAM.com
Mon Jul 31 14:42:34 PDT 2006
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).
I wondered, how would D fare? I started to think if and how that feature
could be implemented in D, as an experimental, proof-of-concept, and
template learning exercise. The first thing needed, I noticed, was a
compile time reflection mechanism, for functions at least (to find it's
parameter types). With some IFTI incantations this is actually already
possible, but with the number of the funtion's parameters limited to
some point(this is what TomS's funcmeta does).
So, I've started out and I got a near complete implementation, but
... then I've just noticed I need one more thing: the ability to call a
function with a constant but parameterized number of arguments. That is,
I have a:
Compile time parameter which is the number of arguments:
N
The arguments:
Object[N] objar;
An array-like template that provides the exact (class) type of each of
those arguments:
funcTypeINFO.arg!(int n)
And then I would need to call a function like this:
dispatchfn(
cast(funcTypeINFO.arg!(0)) objar[0],
cast(funcTypeINFO.arg!(1)) objar[1],
...
cast(funcTypeINFO.arg!(N)) objar[N]
);
Note that dispatchfn is not a variadic function, it is a normal function
with N parameters.
Is this possible? I fear it is not, at least without entering into
ABI-dependent hackery, which I would like to avoid :/ (but which I guess
is still better than nothing).
--
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d-learn
mailing list