How to know whether to use function or delegate

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Jul 17 01:27:10 PDT 2006


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.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d-learn mailing list