rationale for function and delegate

dsimcha dsimcha at yahoo.com
Sat Oct 16 07:42:13 PDT 2010


== Quote from Paulo Pinto (pjmlp at progtools.org)'s article
> Hi,
> while reading TDPL I start wondering what is the background between function
> and delegate.
> They seem to provide more or less the same funcionality, except delegate
> allows the capture
> of the function declaration environment.
> Most of the programming languages with support for closures only have one
> way of doing it.
> Why is D providing two ways of doing it? For me sounds like a feature
> similar to register, or
> inline for doing what should be the compilers work. Deciding the best
> implementation for the
> closure.
> Thanks,
> Paulo

Yes, it's ugly but in a systems language you need control over details like
whether a context pointer is present in addition to the function pointer.  Anyhow,
one thing that people always seem to fail to notice is that std.functional has a
toDelegate() function that can convert just about any function pointer into a
delegate with minimal overhead.  This mitigates the situation a lot, since if an
API requires a delegate and you have a function pointer, you just do a
toDelegate(someFunctionPointer).


More information about the Digitalmars-d mailing list