rationale for function and delegate

Steven Schveighoffer schveiguy at yahoo.com
Sat Oct 16 08:57:36 PDT 2010


On Sat, 16 Oct 2010 11:22:43 -0400, Juanjo Alvarez <fake at fakeemail.com>  
wrote:

> On Sat, 16 Oct 2010 14:42:13 +0000 (UTC), dsimcha <dsimcha at yahoo.com>  
> wrote:
>> delegate with minimal overhead.  This mitigates the situation a
> lot, since if an
>> API requires a delegate and you have a function pointer, you just
> do a
>> toDelegate(someFunctionPointer).
>
> Sorry for asking here something that should go to D.learn, but how do  
> you do the reverse, that is, getting a function from a delegate? I need  
> that in my project so I can pass it to signal so some Unix signal will  
> trigger a method of an already instantiated object.

auto dg = &obj.method;

auto fptr = dg.funcptr;
auto context = dg.ptr;

Note, you cannot call fptr, you will get a runtime error.

Here is the related documentation (search for funcptr):  
http://www.digitalmars.com/d/2.0/function.html

I believe there may be in phobos a type which wraps a function pointer  
into a delegate.  Not sure if it was ever added though...

-Steve


More information about the Digitalmars-d mailing list