delegates vs functions => practical consequences

Xan xancorreu at gmail.com
Thu Apr 19 06:04:47 PDT 2012


On Thursday, 19 April 2012 at 11:46:59 UTC, Steven Schveighoffer 
wrote:
> 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


Thank you very much all of you for the information. Now I have an 
idea of practical benefits and contra-benefits of these.

By the other hand, is there toFunction for passing delegate to 
function (theorically it's possible, isn't?)

Thanks,
Xan.


More information about the Digitalmars-d-learn mailing list