delegates vs functions => practical consequences

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 19 04:46:58 PDT 2012


On Wed, 18 Apr 2012 17:07:07 -0400, Xan <xancorreu at gmail.com> wrote:

> Hi,
>
> I want to know what is most interesting for me: delegates or functions.  
> I consulted sources but none say the practical consequences of such  
> election.
>
> What can I do and what can't I do with functions and delegates?
> Please, be didactics, I'm a newbee

In my experience, delegates are the more useful type to *store*.  I've  
implemented this in some places:

int delegate(int) stored_dg;

void setDelegate(int delegate(int) dg) { stored_dg = dg; }
void setDelegate(int function(int) fn) { stored_dg =  
std.functional.toDelegate(fn); }

On the whole, delegates are slightly more expensive to call, but not by  
much.  However, a function converted to a delegate pays the penalty of a  
double call, because it takes a call to strip out the context pointer.  I  
wish there was a more straightforward way to do this...

But I've not seen this be a huge factor -- yet.

-Steve


More information about the Digitalmars-d-learn mailing list