Is it possible to convert a delegate into a function?

Chris Williams yoreanon-chrisw at yahoo.co.jp
Thu Feb 6 14:17:08 PST 2014


On Thursday, 6 February 2014 at 21:26:06 UTC, Gary Willoughby 
wrote:
> I know you can convert functions into delegates using: 
> http://dlang.org/phobos/std_functional.html#.toDelegate but can 
> you do this the other way around?
>
> I have a method that needs a function pointer but it would be 
> nice to use a converted delegate pointer instead.

A function is the address of a block of code in memory. A 
delegate also has the address to a block of code (a function) but 
it also has the pointer to an object that is to be passed as the 
first parameter to the function.

It's pretty easy to drop a parameter, so writing a wrapper method 
that passes all the other parameters on to the target function is 
pretty easy.

But unless the function that you're calling can accept a 
reference to the object that the delegate references and use that 
and the function pointer to reconstruct the delegate, you're 
definitely out of luck. Whether there is a means to manually 
construct a delegate or not, I'm not sure.


More information about the Digitalmars-d-learn mailing list