How to know whether to use function or delegate

Reiner Pope reiner.pope at gmail.com
Mon Jul 17 03:56:12 PDT 2006


Kirk McDonald wrote:
> Reiner Pope wrote:
>> When making a program with a callback, how do you decide whether to 
>> use a function or a delegate? E.g,
>>
>>   void foo(void function(int) consume)
>>   { ... }
>>
>> or
>>
>>   void foo(void delegate(int) consume)
>>   { ... }
> 
> Delegates are functions with context. They are therefore more useful in 
> the general case. (It is easier to turn a function into a delegate than 
> vice-versa.) However, which one you want depends on your use-case. When 
> in doubt, I would default to using a delegate. This allows the use of 
> lambda functions as callbacks, if nothing else.
> 
> Walter has said on a number of occasions that he eventually (as in, 2.0) 
> wants to make function pointers and delegates the same thing, which will 
> make the question moot.
> 

Thanks for the response. I thought that was the case, but I didn't 
really see why functions would really be in the spec in that case.



More information about the Digitalmars-d-learn mailing list