Convert a delegate to a function (i.e. make a thunk)

Alex Rønne Petersen xtzgzorex at gmail.com
Wed Apr 25 14:42:00 PDT 2012


On 25-04-2012 23:27, Jonathan M Davis wrote:
> On Wednesday, April 25, 2012 14:02:53 Mehrdad wrote:
>> It would be nice if there was a way to convert delegates to
>> functions/thunks, because certain annoying tasks (e.g. 'WndProc's in
>> Windows) would become a heck of a lot easier.
>>
>> Is there any way to already do this? If not, how about adding a toFunction()
>> method in std.functional?
>>
>> (This would of course require making data executable, so that's why it's
>> painful to get right. ATL uses a pool of some sort, I think... so if
>> toFunction() did this internally, with a separate pool, it would reduce a
>> lot of memory management burden from the programmer.)
>
> You could have a function which took a delegate and called it (which could
> probably be done with void* if the function has to be an extern(C) function),
> or if the function didn't need to be pure, then you could have a function
> which called a module-level variable which the delegate had been assigned to.
> But you can't convert a delegate to a function without passing the delegate to
> the function one way or another, because the function doesn't have a context
> (not being a delegate).
>
> - Jonathan M Davis

http://en.wikipedia.org/wiki/Thunk_(functional_programming)
http://catb.org/jargon/html/T/thunk.html

You may be able to do something like this with libffi.

(See https://github.com/lycus/libffi-d for a D binding.)

-- 
- Alex


More information about the Digitalmars-d mailing list