repost: semantics of the keyword lazy

Derek Parnell derek at nomail.afraid.org
Wed Oct 11 21:12:57 PDT 2006


On Thu, 12 Oct 2006 03:08:07 +0000 (UTC), Karen Lanrap wrote:

> I have posted the following in the learn-group without any answers:
> 
> From the documentation:
> 
> Lazy arguments are evaluated not when the function is called, but 
> when the parameter is evaluated within the function.
> 
> First to note, that this is a circular definition without any fixed 
> point to start from:
> a lazy argument is evaluated, when it is evaluated :-(

I have not read that way at all. To me this is not a circular definition. 

Non-lazy arguments are evaluated by the code that initiates the call to the
function. Lazy ones are evaluated by code that lies inside the function;
that is they are evaluated after the function is called, by your code in
the function.

> Maybe Walter meant something like this:
> With the exception of lazy parameters all actual parameters are 
> passed evaluated to the function they belong to.

That is exactly what he meant and said.
 
> But this does not help for lazy parameters: when the hell are they 
> evaluated?

Whenever the called function decides to do it, if at all. To evaluate a
lazy argument, the function must explicitly call the delegate (which the
lazy argument represents).

> "Within the function" Walters says. But what does this mean if the 
> calling function is recursive or combined with parameters that are 
> not lazy evaluated? Example:
> 
> int ack( lazy bool b, int x, lazy int y){
>     if( x == 0 )
>         if( b) return y+1;
>     else
>         if( y == 0)
>             return ack( b, x-1, 1);
>         else
>             return ack( b, x-1, ack( b, x, y-1));
> }
> 
> If Walter wanted to express, that lazy parameters must be evaluated 
> in the calling function, then I understand, but I doubt, that this 
> concept is that useful.

But that is exactly what they are. It just means that the function
evaluates them by treating them as delegates and calling them, using the
result as the evaluated value.
 
> If on the other hand lazy parameters can be passed down through 
> function hierarchies one should be able to protect them from 
> accidental evaluation until they reach the target point of their 
> evaluation

'accidental'? They are passed down as delegates and only evaluated if
explicitly called. No accident.


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
12/10/2006 1:49:14 PM



More information about the Digitalmars-d mailing list