Thread overview
[phobos] Converting function pointers to delegates
Jan 31, 2010
David Simcha
Jan 31, 2010
Walter Bright
January 30, 2010
A pretty significant wart of D is that function pointers and delegates are completely incompatible.  I've come up with a good hack to convert function pointers to delegates without any heap allocations or additional storage besides the delegate itself being used.  In particular, there are no heap allocations, closures, etc. involved. However, calling a delegate created from a function pointer does still incur two indirect function calls instead of one.  I've attached the code.  Tell me what you think.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: delegateFromFp.d
URL: <http://lists.puremagic.com/pipermail/phobos/attachments/20100130/648e4165/attachment.asc>
January 30, 2010
Looks good. Long term the conversion function -> delegate should be put in the language, but David's code seems to do the trick for the time being.

I'll leave this up to Walter. Walter, do you have the time to implement
  the function -> delegate implicit conversion? If not, I suggest we let
David add his code to Phobos, either in std.typecons or std.functional.
One personal preference: instead of delegateFromFunc I prefer toDelegate
as it's a tad more in sync with other names.

Thanks David!


Andrei

David Simcha wrote:
> A pretty significant wart of D is that function pointers and delegates are completely incompatible.  I've come up with a good hack to convert function pointers to delegates without any heap allocations or additional storage besides the delegate itself being used.  In particular, there are no heap allocations, closures, etc. involved. However, calling a delegate created from a function pointer does still incur two indirect function calls instead of one.  I've attached the code.  Tell me what you think.
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
January 30, 2010
Language support for it won't be for a while, so adding it to Phobos is more appropriate for now.

Andrei Alexandrescu wrote:
> Looks good. Long term the conversion function -> delegate should be put in the language, but David's code seems to do the trick for the time being.
>
> I'll leave this up to Walter. Walter, do you have the time to implement  the function -> delegate implicit conversion? If not, I suggest we let David add his code to Phobos, either in std.typecons or std.functional. One personal preference: instead of delegateFromFunc I prefer toDelegate as it's a tad more in sync with other names.
>
> Thanks David!
>
>
> Andrei
>
> David Simcha wrote:
>> A pretty significant wart of D is that function pointers and delegates are completely incompatible.  I've come up with a good hack to convert function pointers to delegates without any heap allocations or additional storage besides the delegate itself being used.  In particular, there are no heap allocations, closures, etc. involved.  However, calling a delegate created from a function pointer does still incur two indirect function calls instead of one. I've attached the code.  Tell me what you think.
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> phobos mailing list
>> phobos at puremagic.com
>> http://lists.puremagic.com/mailman/listinfo/phobos
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>
>