Converting function to delegate - was Re: How to know whether to use function or delegate

Chris Miller chris at dprogramming.com
Sat Jul 22 08:47:43 PDT 2006


On Sat, 22 Jul 2006 04:16:00 -0400, Walter Bright  
<newshound at digitalmars.com> wrote:

> Chris Nicholson-Sauls wrote:
>> (If there is a hack or workaround to pass a delegate as a function  
>> pointer, I want to see that!)
>
> The only way to do it is to generate a "thunk" of executable code at  
> runtime.

But wouldn't it be a lot easier to convert function to delegate? (even  
without generating anything at runtime?) It seems to me that this is what  
most people want. I don't think it would be bad having just the one  
conversion for a while (or even forever; delegates just have more  
information, you wouldn't provide a thunk to store a long in an int).

For example, people ask whether or not to use delegates or function  
pointers for callbacks. They want the convenience of delegates, but they  
also don't want to force people to use nested functions or member  
functions, but also want to allow using global and static functions. When  
they ask me this question I tell them to go with delegates and give them  
this tip on how to make a "global delegate":
    struct Dummy
    {
       void myfunction()  { ... }
    }
    Dummy funcs;
    mydelegate = &funcs.myfunction; // <--
Isn't there an easier way to directly convert a function to a delegate?



More information about the Digitalmars-d mailing list