lazy evaluation

Tyler Knott tywebmail at mailcity.com
Fri Jun 1 13:26:34 PDT 2007


Pierre Habouzit wrote:
>   lazy types are supported through the Lazy module, and forcing the
> evaluation of a lazy type is done through Lazy.force expr rather than
> expr() like in D. Though, like you can see, once forced, the lazy
> expression sees its value memoized.
> 
>   I'm wondering:
>   * why lazy types in D don't work like that for lazy parameters,
>     because it's really what makes sense and is the most predictible
>     behaviour ;
>   * also why it's not a generic type attribute either and only used as a
>     function parameter. Not knowing the implementation gory details, I
>     don't know if it makes sense at all anyway.
> 
> 

In D lazy parameters are implemented as anonymous delegates.  This means that

void baz(lazy int i)
{
	writefln("baz %d", i);
}

is transformed to

void baz(int delegate() i)
{
	writefln("baz %d", i());
}

where the delegate i references is whatever code you used for that argument.  So 
in your example each time i evaluated you call an anonymous function that calls 
foo() and returns its return value.  If you want the value to stay consistent 
between evaluations of i use a temporary variable to store the result of it or 
make it non-lazy.



More information about the Digitalmars-d mailing list