www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Proof of Concept: Implementing Multiple Dispatch in D (need vararg

reply Bruno Medeiros <brunodomedeirosATgmail SPAM.com> writes:
I watched some time ago a Google tech about Common Lisp ( 
http://video.google.com/videoplay?docid=448441135356213813&q=peter+seibel
) where the Lispanatic speaker exalts LISP and whines about Java.

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 could 
be implemented in D, as a proof-of-concept, D testing, 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 parameters limited (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 arguments.
That is I have a:
Compile time constant (template param) which is the number of args:
   N
The arguments:
   Object[N] objar;
An array-like template that provides the (class) type of each argument 
of the function to be called:
   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]
   );

Is this possible? I fear this is not possible without entering into 
ABI-dependent hackery :/ which I would like to avoid (but is better than 
nothing).




-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Jul 31 2006
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Bruno Medeiros wrote:
 I watched some time ago a Google tech about Common Lisp ( 
 http://video.google.com/videoplay?docid=448441135356213813&q=peter+seibel
 ) where the Lispanatic speaker exalts LISP and whines about Java.
 
 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 could 
 be implemented in D, as a proof-of-concept, D testing, 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 parameters limited (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 arguments.
 That is I have a:
 Compile time constant (template param) which is the number of args:
   N
 The arguments:
   Object[N] objar;
 An array-like template that provides the (class) type of each argument 
 of the function to be called:
   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]
   );
 
 Is this possible? I fear this is not possible without entering into 
 ABI-dependent hackery :/ which I would like to avoid (but is better than 
 nothing).
 

Sounds like you want tuples. Tom S's 'bind' module might do what you're looking for. Search digitalmars.D.announce for the thread 'A library similar to boost::bind'. Pyd's function wrapping abillities are vaugely similar to this, too. The code isn't very pretty: It's limited to 10 arguments for a given function, is annoying to read (there are some bits that don't really work that I haven't bothered to remove, yet), and is an eyesore in places. It does work, however. :-) The Pyd source: http://dsource.org/projects/pyd/browser/trunk/infrastructure/pyd The function wrapping module itself: http://dsource.org/projects/pyd/browser/trunk/infrastructure/pyd/func_wrap.d Pyd's tuple module: http://dsource.org/projects/pyd/browser/trunk/infrastructure/pyd/tuples.d -- Kirk McDonald Pyd: Wrapping Python with D http://dsource.org/projects/pyd/wiki
Jul 31 2006
parent Tom S <h3r3tic remove.mat.uni.torun.pl> writes:
Kirk McDonald wrote:
 Bruno Medeiros wrote:
 I watched some time ago a Google tech about Common Lisp ( 
 http://video.google.com/videoplay?docid=448441135356213813&q=peter+seibel
 ) where the Lispanatic speaker exalts LISP and whines about Java.

 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 could 
 be implemented in D, as a proof-of-concept, D testing, 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 parameters limited (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 arguments.
 That is I have a:
 Compile time constant (template param) which is the number of args:
   N
 The arguments:
   Object[N] objar;
 An array-like template that provides the (class) type of each argument 
 of the function to be called:
   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]
   );

 Is this possible? I fear this is not possible without entering into 
 ABI-dependent hackery :/ which I would like to avoid (but is better 
 than nothing).

Sounds like you want tuples. Tom S's 'bind' module might do what you're looking for. Search digitalmars.D.announce for the thread 'A library similar to boost::bind'.

Yup, I think you might like my Tuple and Apply modules from the Bind lib: www.mat.uni.torun.pl/~h3r3tic/bind.rar -- Tomasz Stachowiak
Jul 31 2006