Learning delegates
Bert
Bert at gmail.com
Tue Sep 10 07:21:12 UTC 2019
On Sunday, 8 September 2019 at 10:04:57 UTC, Joel wrote:
> I'm trying to understand delegates. Is there any good ways I
> can get a better understanding of them?
Simple, don't make it harder than it is.
Delegates are basically functions... that is, function
pointers(they point to some function somewhere in space)... BUT
they include a "context". The context a scope.
{ // In some scope
int x;
d = () { writeln(x); };
}
() { writeln(x); };
is the function defined as a lambda(inline).
It accesses a variable outside of it, that is, in the scope...
which is called the context.
d is the delegate, it is a function pointer that holds the
function AND the context pointer.
We can then do
d();
which called/executes the function... the function is called, and
x can be referenced because d stores the context.
If you do not understand functions, then function pointers, you
can't understand delegates.
More information about the Digitalmars-d-learn
mailing list