Thread overview
Passing lazy parmeters to functions that take lazy parameters
Sep 04, 2006
Joe Gottman
Sep 04, 2006
Walter Bright
Sep 04, 2006
Joe Gottman
Sep 04, 2006
Walter Bright
September 04, 2006
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


September 04, 2006
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.
September 04, 2006
"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


September 04, 2006
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?
>> 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?

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.
September 04, 2006
Joe Gottman wrote:
> OK.  So what's the best way to force evaluation?  Would just mentioning it in a line by itself work?

Yes.
September 05, 2006
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.