www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Delegate

reply =?ISO-8859-15?Q?Fabian_Cla=DFen?= <admin fabs-world.de> writes:
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 8bit

Hi
I have a problem:

What's wrong about this code?
Code:

import std.stdio;

int main() {
	writefln("%f", add(100, 786, 56.0));
	return 0;
}

double add(double delegate()[] dgs...) {
	double result = 0;
	foreach(double delegate() dg; dgs) {
		result += dg;
	}
	return result;
}

Or a better question is: How can I get the value out of the delegate?

Greetings Fabian Claßen

PS: I am a beginner :P
Dec 02 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Dec 2, 2008 at 1:30 PM, Fabian Cla=DFen <admin fabs-world.de> wrote=
:
                result +=3D dg;

Just change that line to "result +=3D dg();". A delegate is like a function; you call it to get the value.
Dec 02 2008
parent reply =?ISO-8859-1?Q?Fabian_Cla=DFen?= <admin fabs-world.de> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Jarrett Billingsley schrieb:
 On Tue, Dec 2, 2008 at 1:30 PM, Fabian Claßen <admin fabs-world.de> wrote:
                result += dg;

Just change that line to "result += dg();". A delegate is like a function; you call it to get the value.

Oh thank you. I see. That's a stupid mistake of mine :D But I love D.
Dec 02 2008
parent Janderson <ask me.com> writes:
Fabian Claßen wrote:
 Jarrett Billingsley schrieb:
 On Tue, Dec 2, 2008 at 1:30 PM, Fabian Claßen <admin fabs-world.de> 
 wrote:
                result += dg;

Just change that line to "result += dg();". A delegate is like a function; you call it to get the value.

Oh thank you. I see. That's a stupid mistake of mine :D But I love D.

Great Stuff. We where all at this point at some time. Its fine that you posted to this group. However you might try posting your questions to D.learn next time, simply so other people who have the same problem can easily find the solution. ;) -Joel
Dec 02 2008
prev sibling next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 02 Dec 2008 19:30:47 +0100, Fabian Claßen wrote:

 Hi
 I have a problem:
 
 What's wrong about this code?
 Code:
 
 import std.stdio;
 
 int main() {
 	writefln("%f", add(100, 786, 56.0));
 	return 0;
 }
 
 double add(double delegate()[] dgs...) {
 	double result = 0;
 	foreach(double delegate() dg; dgs) {
 		result += dg;
 	}
 	return result;
 }
 
 Or a better question is: How can I get the value out of the delegate?

I don't understand your code, sorry. It seems to me that the argument you supplied to add() is a list of doubles and not a list of delegates. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 02 2008
next sibling parent =?ISO-8859-1?Q?Fabian_Cla=DFen?= <admin fabs-world.de> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Jarrett Billingsley schrieb:
 On Tue, Dec 2, 2008 at 2:45 PM, Derek Parnell <derek psych.ward> wrote:
 On Tue, 02 Dec 2008 19:30:47 +0100, Fabian Claßen wrote:

 Hi
 I have a problem:

 What's wrong about this code?
 Code:

 import std.stdio;

 int main() {
       writefln("%f", add(100, 786, 56.0));
       return 0;
 }

 double add(double delegate()[] dgs...) {
       double result = 0;
       foreach(double delegate() dg; dgs) {
               result += dg;
       }
       return result;
 }

 Or a better question is: How can I get the value out of the delegate?

supplied to add() is a list of doubles and not a list of delegates.

It's legal. If you have a typesafe variadic array of delegates, you can call the function as if it took a bunch of lazy parameters.

I am learning D. But ... I worked with other programming languages before and so I learn D with a book for experienced programmers. My learning concept is like: reading ... memorizing ... coding So there was a very similar code in the book and I tried to write this code by heart. That's only for your information why I've writen the code like this. The function gets a list of lazy arguments and summarizes the arguments. Greetings Fabian Claßen PS: Sorry I believe my english is bad - I am German :D lol
Dec 02 2008
prev sibling parent Derek Parnell <derek psych.ward> writes:
On Tue, 02 Dec 2008 22:53:25 +0300, Denis Koroskin wrote:

 On Tue, 02 Dec 2008 22:45:03 +0300, Derek Parnell <derek psych.ward> wrote:
 
 On Tue, 02 Dec 2008 19:30:47 +0100, Fabian Claßen wrote:

 Hi
 I have a problem:

 What's wrong about this code?
 Code:

 import std.stdio;

 int main() {
 	writefln("%f", add(100, 786, 56.0));
 	return 0;
 }

 double add(double delegate()[] dgs...) {
 	double result = 0;
 	foreach(double delegate() dg; dgs) {
 		result += dg;
 	}
 	return result;
 }

 Or a better question is: How can I get the value out of the delegate?

I don't understand your code, sorry. It seems to me that the argument you supplied to add() is a list of doubles and not a list of delegates.

I was a bit surprized at first, too, but any value is indeed a lazy delegate that returns itself.

And they C++ has some arcane rules :-)!!! -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Dec 02 2008
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Tue, 02 Dec 2008 22:45:03 +0300, Derek Parnell <derek psych.ward> wrote:

 On Tue, 02 Dec 2008 19:30:47 +0100, Fabian Claßen wrote:

 Hi
 I have a problem:

 What's wrong about this code?
 Code:

 import std.stdio;

 int main() {
 	writefln("%f", add(100, 786, 56.0));
 	return 0;
 }

 double add(double delegate()[] dgs...) {
 	double result = 0;
 	foreach(double delegate() dg; dgs) {
 		result += dg;
 	}
 	return result;
 }

 Or a better question is: How can I get the value out of the delegate?

I don't understand your code, sorry. It seems to me that the argument you supplied to add() is a list of doubles and not a list of delegates.

I was a bit surprized at first, too, but any value is indeed a lazy delegate that returns itself.
Dec 02 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Tue, Dec 2, 2008 at 2:45 PM, Derek Parnell <derek psych.ward> wrote:
 On Tue, 02 Dec 2008 19:30:47 +0100, Fabian Cla=DFen wrote:

 Hi
 I have a problem:

 What's wrong about this code?
 Code:

 import std.stdio;

 int main() {
       writefln("%f", add(100, 786, 56.0));
       return 0;
 }

 double add(double delegate()[] dgs...) {
       double result =3D 0;
       foreach(double delegate() dg; dgs) {
               result +=3D dg;
       }
       return result;
 }

 Or a better question is: How can I get the value out of the delegate?

I don't understand your code, sorry. It seems to me that the argument you supplied to add() is a list of doubles and not a list of delegates.

It's legal. If you have a typesafe variadic array of delegates, you can call the function as if it took a bunch of lazy parameters.
Dec 02 2008