lazy evaluation

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Jun 4 01:26:10 PDT 2007


Pierre Habouzit wrote:
>   As a general rule, in many complex applications, you often have
> somehow complex operations (parse, compilation, hashing, whatever) that
> you do "just in case", because it's easier to suppose this operation has
> been done unconditionnaly, rather than using everywhere you need the
> parse/compilation/hash/... available things like:
> 
>   if (foo->did_i_parsed_it())
>       foo->parse();
>   use_parse_of_foo(foo->parseresult);
> 
>   Because one day, you'll forget about it. If you have the lazy
> expressions I'm talking about, then instead of doing the real parse in
> your program, you just need to write the lazy expression that once
> evaluated will compute the parse/compilation/whatever.
> 
>   Then you use your foo->parseresult member as a lazy expression, that
> will be evaluated iff your application really needed it.

How about something like this:
---
class Foo {
     private bool parsed;
     private Tree parseresult_;
     Tree parseresult() { if (!parsed) parse(); return parseresult; }
     private void parse() { parseresult_ = ...; }
}
---
Then you can just use foo.parseresult without needing to explicitly 
re-check every time.
Properties are very handy for stuff like this.

>   Though, maybe this don't need to be done with the D lazy construct
> though, but I didn't came with a satisfying implementation yet. (I mean
> one that has a light enough syntax for the lazy expression use).

I think something like the above code combined with passing 
foo.parseresult as a lazy parameter might do pretty much what you're 
looking for.



More information about the Digitalmars-d mailing list