digitalmars.D.learn - Learning delegates
- Joel (2/2) Sep 08 2019 I'm trying to understand delegates. Is there any good ways I can
- Max Samukha (3/5) Sep 08 2019 You may want to read this:
- berni (3/5) Sep 08 2019 I wrote a foreach loop using opApply. A side effect of that was,
- =?UTF-8?B?UsOpbXkgTW91w6t6YQ==?= (26/28) Sep 08 2019 I am no compiler implementer, so what is below may contain a lot
- bachmeier (3/5) Sep 09 2019 I think this chapter should give you some useful information:
- Bert (21/23) Sep 10 2019 Simple, don't make it harder than it is.
I'm trying to understand delegates. Is there any good ways I can get a better understanding of them?
Sep 08 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?You may want to read this: https://tour.dlang.org/tour/en/basics/delegates
Sep 08 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?I wrote a foreach loop using opApply. A side effect of that was, that after I managed to do this I understood delegates. :-)
Sep 08 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?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");
Sep 08 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?I think this chapter should give you some useful information: http://www.ddili.org/ders/d.en/lambda.html
Sep 09 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.
Sep 10 2019