Library writing - delegates or fp's?

Lionello Lunesu lio at lunesu.remove.com
Wed Jun 21 06:36:13 PDT 2006


Chad J wrote:
> So I have decided to roll my own GUI library, one dedicated to computer 
> games.  But I've run into a bit of an unpleasant design problem - for 
> handling callbacks, how should my library expose function pointers and 
> delegates?
> 
> I suppose I could choose one or the other, but I think it desirable to 
> allow use of both since I don't want to force the user of the library 
> into a particular style or make them use ugly workarounds.  I am 
> currently thinking of a few possible solutions, none of which look very 
> pleasing to me:

I'd pick delegates.

> #1:  Force them to use delegates and work around it to do module-level 
> programming.  Example -

At the moment, a function can't be cast to a delegate, but I think this 
will change in the future (since it's theoretically possible). In the 
mean time you can use the new lambda/literal syntax:

int module_level_func() { return 1; }

void somefunc( int delegate() callback ) { callback(); }

void main() {
//  somefunc( &module_level_func );// not (yet) possible
   somefunc( { return module_level_func();} ); //wrap it
}

L.



More information about the Digitalmars-d-learn mailing list