View previous topic :: View next topic |
Author |
Message |
Nova
Joined: 18 Sep 2004 Posts: 23
|
Posted: Mon Dec 13, 2004 1:14 pm Post subject: what are delegates? |
|
|
I was reading about functions versus delegates on a wiki page but I still don't really see the full concept.
What are the differences between a function and a delegate? |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
|
Back to top |
|
|
Nova
Joined: 18 Sep 2004 Posts: 23
|
Posted: Mon Dec 13, 2004 9:53 pm Post subject: |
|
|
Thanks, that gives me a bit of an idea. What would that be good for? I think a good solid example would help me picture it more clearly. |
|
Back to top |
|
|
Ant
Joined: 06 Mar 2004 Posts: 306 Location: Canada
|
Posted: Mon Dec 13, 2004 10:27 pm Post subject: delegate example |
|
|
Nova wrote: | Thanks, that gives me a bit of an idea. What would that be good for? I think a good solid example would help me picture it more clearly. |
I think an excelent example is the listDir from phobos.src.file
by Vathix if I remember correctly.
the delegate is the inner function listing.
of course, I don't approve the use of 'continue' or 'break'
on a while loop.
(BTW, Vathix,
This would be much more usefull if the parameter for the delegate was a full file descriptor - the stat struct- instead of just the file name. That's how I'm going to change it on the next revision of my implementation)
Ant
/***************************************************
* Return contents of directory.
*/
char[][] listdir(char[] pathname)
{
. char[][] result;
. bool listing(char[] filename)
. {
. result ~= filename;
. return true; // continue
. }
. listdir(pathname, &listing);
. return result;
}
void listdir(char[] pathname, bool delegate(char[] filename) callback)
{
. DIR* h;
. dirent* fdata;
. h = opendir(toStringz(pathname));
. if (h) // if !h, should we throw exception?
. {
. while((fdata = readdir(h)) != null)
. {
. // Skip "." and ".."
. if (!std.string.strcmp(fdata.d_name, ".") ||
. !std.string.strcmp(fdata.d_name, ".."))
. continue;
. int len = std.string.strlen(fdata.d_name);
. if (!callback(fdata.d_name[0 .. len].dup))
. break;
. }
. closedir(h);
. }
} |
|
Back to top |
|
|
Nova
Joined: 18 Sep 2004 Posts: 23
|
Posted: Mon Dec 13, 2004 10:46 pm Post subject: |
|
|
So char[][] result; gets shared in the delegate function in listing()? The delegate isn't in its own scope? |
|
Back to top |
|
|
Ant
Joined: 06 Mar 2004 Posts: 306 Location: Canada
|
Posted: Tue Dec 14, 2004 12:42 am Post subject: |
|
|
Nova wrote: | So char[][] result; gets shared in the delegate function in listing()? The delegate isn't in its own scope? |
Sorry, revisiting your original question I realize that my answer is actually irrelevante.
The delegate contains the information for the function and for it's context.
Usually (for me) that would be a class instance as the context and a method of that class as the function.
Ant |
|
Back to top |
|
|
Nova
Joined: 18 Sep 2004 Posts: 23
|
Posted: Tue Dec 14, 2004 1:21 pm Post subject: |
|
|
So could I think of a delegate as a virtual function that shares the context of where it is with itself? If not, don't blast me for my stupidity x_x |
|
Back to top |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Wed Dec 15, 2004 8:31 am Post subject: |
|
|
A delegate is simply a function pointer which carries its context (Be it a class, instance, stack frame, or what have you) with it. For example...
Given the following
Code: |
class Foo {
void bar() { }
}
Foo foo = new Foo;
|
The following are identical
Code: |
int delegate() dg = &(foo.bar);
dg();
|
The advantage in this case is that the delegate can be passed around like any other referance variable. _________________ Chris Nicholson-Sauls |
|
Back to top |
|
|
Nova
Joined: 18 Sep 2004 Posts: 23
|
Posted: Wed Dec 15, 2004 3:07 pm Post subject: |
|
|
I get it!
Thank you so much csauls. |
|
Back to top |
|
|
|