Library writing - delegates or fp's?

Andrei Khropov andkhropov at nospam_mtu-net.ru
Tue Jul 18 14:44:52 PDT 2006


BCS wrote:

> 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?
> > 
> [...]
> 
> a while back someone found out that you can take a delegate and replace the
> internal function pointer with a regular function pointer and it still works
> just fine. Given this, you could wright a little function*-to-delegate
> function and let people do this
> 
> 
> UseThisCallback(int delegate(int));
> 
> int delegate(int) f2d(int function(int) fnp){...}
> 
> 
> int fn(int);
> 
> void main()
> {
> 	UseThisCallback(f2d(&fn));
> }
> 
> 
> I known this is an ugly hack, OTOH maybe Walter can be convinced to add a
> function-pointer to delegate cast that does this in a consistent manner.
> 
> cast(int delegate(int))&fn

maybe just "cast(delegate)&fn" in the spirit of inferred typing?


-- 




More information about the Digitalmars-d-learn mailing list