Thread overview
Learning delegates
Sep 08
Joel
Sep 08
berni
Sep 09
bachmeier
Sep 10
Bert
September 08
I'm trying to understand delegates. Is there any good ways I can get a better understanding of them?
September 08
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?

You may want to read this: https://tour.dlang.org/tour/en/basics/delegates
September 08
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?

I wrote a foreach loop using opApply. A side effect of that was, that after I managed to do this I understood delegates. :-)
September 08
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?


I am no compiler implementer, so what is below may contain a lot of inaccuracies and conceptual shortcuts, but here is my view of delegates in D.  I hope this helps.

Delegates are fat function pointers.

D arrays are also fat function pointers: they can be implemented as a struct with a size_t length and a pointer to the data:

    sruct DArray(T) {
        size_t length;
        T * data;
    }

D delegates can be implemented as a pointer to some context data and a function pointer, something similar to D arrays:

    struct DDelegate(Context, Return, Args) {
        Context context;
        Return function(Args) functionPointer;
    }

The context can be:
- a struct value
- a class instance
- some data from a local function frame when the delegate is used as a closure.

The compiler replaces a call to the delegate in the source code by a call to the function pointer with the right data for runtime.
Something like:

    dg.functionPointer(dg.context, "hello, world");




September 09
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?

I think this chapter should give you some useful information:
http://www.ddili.org/ders/d.en/lambda.html
September 10
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.