www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Passing lazy parmeters to functions that take lazy parameters

reply "Joe Gottman" <jgottman carolina.rr.com> writes:
Suppose I have a function f that has a lazy parameter x, and I pass it into 
another function g that has a lazy parameter.  Will x be evaluated when it 
is passed to g?  For instance:

void g(lazy int x) {/* Do nothing */}

void f (lazy int x) {
    g(x);  //Is x evaluated here?
}

void main()
{
   int x = 0;
   f(++x);
    printf("%d\n", x); //Do we print 0 or 1?
}

Joe Gottman 
Sep 04 2006
parent reply Walter Bright <newshound digitalmars.com> writes:
Joe Gottman wrote:
 Suppose I have a function f that has a lazy parameter x, and I pass it into 
 another function g that has a lazy parameter.  Will x be evaluated when it 
 is passed to g?

No, it isn't evaluated. The evaluation of it is wrapped up into another delegate and passed to g.
Sep 04 2006
next sibling parent reply "Joe Gottman" <jgottman carolina.rr.com> writes:
"Walter Bright" <newshound digitalmars.com> wrote in message 
news:edi8s6$olu$1 digitaldaemon.com...
 Joe Gottman wrote:
 Suppose I have a function f that has a lazy parameter x, and I pass it 
 into another function g that has a lazy parameter.  Will x be evaluated 
 when it is passed to g?

No, it isn't evaluated. The evaluation of it is wrapped up into another delegate and passed to g.

OK. So what's the best way to force evaluation? Would just mentioning it in a line by itself work? Joe Gottman
Sep 04 2006
next sibling parent =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak utu.fi.invalid> writes:
Joe Gottman wrote:
 "Walter Bright" <newshound digitalmars.com> wrote in message 
 news:edi8s6$olu$1 digitaldaemon.com...
 Joe Gottman wrote:
 Suppose I have a function f that has a lazy parameter x, and I pass it 
 into another function g that has a lazy parameter.  Will x be evaluated 
 when it is passed to g?

delegate and passed to g.

OK. So what's the best way to force evaluation? Would just mentioning it in a line by itself work?

Because g also uses lazy evaluation you have to evaluate x in f before giving it forward as an argument to g. Still, the evaluated value gets wrapped up into a delegate before it's finally usable in g as a simple value. The best way might be not to use lazy evaluation if it's not necessary.
Sep 04 2006
prev sibling parent Walter Bright <newshound digitalmars.com> writes:
Joe Gottman wrote:
 OK.  So what's the best way to force evaluation?  Would just mentioning it 
 in a line by itself work?

Yes.
Sep 04 2006
prev sibling parent "Unknown W. Brackets" <unknown simplemachines.org> writes:
Is it necessary to wrap it in another delegate?

Assuming it already has a delegate which returns int, it seems a 
possible optimization to simply pass this existing delegate on, without 
packaging it further...

Is that not the case, or simply a field for future optimization?

Thanks,
-[Unknown]


 Joe Gottman wrote:
 Suppose I have a function f that has a lazy parameter x, and I pass it 
 into another function g that has a lazy parameter.  Will x be 
 evaluated when it is passed to g?

No, it isn't evaluated. The evaluation of it is wrapped up into another delegate and passed to g.

Sep 04 2006