repost: semantics of the keyword lazy

Karen Lanrap karen at digitaldaemon.com
Thu Oct 12 12:37:15 PDT 2006


Derek Parnell wrote:

> Whenever the called function decides to do it, if at all.

Hopefully the programmer has full control over what the called 
function decides :-(


Another try:

The evaluation of actual parameters can be delayed to any level in 
the call hierarchy.

If the evaluation of an actual parameter has to be delayed by at 
least one level, then the type of the formal parameter has to be 
prefixed by the keyword 'lazy'.

If in a called function 'foo' a formal lazy parameter is passed to 
another called function 'bar' as a reference, i.e. without appended 
parentheses, on a position where a lazy parameter is expected, then 
the actual parameter is not evaluated in 'foo'. In all other cases 
of 
reached usage the actual parameter is evaluated.

Example:
int ack( lazy int x, lazy int y){
    // ...
        return ack( x-1, ack( x, y-1));
    // this actual parameter  ^
    // is not evaluated, but passed down only

    // wheras
        return ack( x-1, ack( x(), y-1));
    // this actual parameter    ^
    // _is_ evaluated and then passed down
}


Did I get that correctly?



More information about the Digitalmars-d mailing list