reddit - lazy evaluation of function arguments

Matthias Walter Matthias.Walter at st.ovgu.de
Sun Jul 13 02:33:34 PDT 2008


renoX Wrote:

> Walter Bright a écrit :
> > vote up!
> > 
> > http://www.reddit.com/info/6pkri/comments/
> 
> There's a drawback with "invisible" laziness: now you cannot know 
> looking at f(<expression>) how many times <expression> may be evaluated 
> without looking at f definition (is-it taking a 'normal' argument or a 
> 'lazy' one).
> 
> With f({return <expression>;}) at least you know that you're sending a 
> piece of code with may be evaluated many times, not only once..
> 
> So from a semantic POV it's clearer, from a syntactic POV, it's ugly: 
> especially the 'return', so taking an idea from Smalltalk if you change 
> 'return' to '^', it becomes more visually pleasant:
> f({^<expression>;});
> the 'syntactic' noise compared to f(<expression>); is bearable I think.
> 
> If the compiler is able to distinguish <expression> with or without 
> side-effect then f(<expression>); could be allowed as a shortcut for 
> f({^<expression>;}); if the <expression> is side-effect free (after all 
> the distinction only matters where there could be side-effects)..

What about f (lazy <expression>) - the caller is forced to either put a real delegate there or a "lazy" prefix before an expression. This is also much clearer than braces, ^ or some other symbol... just an idea :)

best regards
Matthias Walter


More information about the Digitalmars-d-announce mailing list