FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

what are delegates?

 
Post new topic   Reply to topic     Forum Index -> Tutorials
View previous topic :: View next topic  
Author Message
Nova



Joined: 18 Sep 2004
Posts: 23

PostPosted: Mon Dec 13, 2004 1:14 pm    Post subject: what are delegates? Reply with quote

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
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Mon Dec 13, 2004 6:39 pm    Post subject: Reply with quote

Take a look at this www.digitalmars.com/d/function.html#closures
A delegate holds extra information making things a lot easier.
Back to top
View user's profile Send private message
Nova



Joined: 18 Sep 2004
Posts: 23

PostPosted: Mon Dec 13, 2004 9:53 pm    Post subject: Reply with quote

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
View user's profile Send private message
Ant



Joined: 06 Mar 2004
Posts: 306
Location: Canada

PostPosted: Mon Dec 13, 2004 10:27 pm    Post subject: delegate example Reply with quote

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
View user's profile Send private message
Nova



Joined: 18 Sep 2004
Posts: 23

PostPosted: Mon Dec 13, 2004 10:46 pm    Post subject: Reply with quote

So char[][] result; gets shared in the delegate function in listing()? The delegate isn't in its own scope?
Back to top
View user's profile Send private message
Ant



Joined: 06 Mar 2004
Posts: 306
Location: Canada

PostPosted: Tue Dec 14, 2004 12:42 am    Post subject: Reply with quote

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
View user's profile Send private message
Nova



Joined: 18 Sep 2004
Posts: 23

PostPosted: Tue Dec 14, 2004 1:21 pm    Post subject: Reply with quote

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
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Wed Dec 15, 2004 8:31 am    Post subject: Reply with quote

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();

Code:

foo.bar();


The advantage in this case is that the delegate can be passed around like any other referance variable.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
Nova



Joined: 18 Sep 2004
Posts: 23

PostPosted: Wed Dec 15, 2004 3:07 pm    Post subject: Reply with quote

I get it! Very Happy

Thank you so much csauls.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Tutorials All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group