www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.boxer for functions and delegates

Is it possible to create a kind of std.boxer for delegates/functions?

I think this would be very useful for creating signals and slots that 
are more forgiving in their rules for what's connect-able.

Specifically can one make something like this work?

   float float_float(float a) { return 27; }
   float float_void() { return 42; }
   void void_float(float a) { }
   void void_void() { }

   FunctionBoxer ff = funcbox(&float_float);
   FunctionBoxer fv = funcbox(&float_void);
   FunctionBoxer vv = funcbox(&void_void);
   FunctionBoxer vf = funcbox(&void_float);

   FunctionBoxer funcs = [ff,fv,vv,vf];
   foreach(f; funcs) { f(3); }

I think 3 types of flexibility are desirable:

1) Like std.boxer, usual type conversion rules should apply, so calling 
ff(3) with an int instead of a float should be ok.  (It's probably hard 
to not get that behavior)

2) Should be ok to call with *more* arguments than needed (they will 
just be ignored).

3) Should be ok to have a return value or not. (Return type could be a 
template parameter I suppose --  funcbox!(void)(&float_float) )

Nov 13 2006