reddit - lazy evaluation of function arguments

renoX renosky at free.fr
Sat Jul 12 13:43:53 PDT 2008


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)..


Sure some may ask why not allow simply f({<expression>;}); with the last 
expression evaluated being the return value?
A study for the E-language has shown that this leads sometimes to value 
being returned where they shouldn't so it's a bad idea from 
maintainability view and security (if the object leaked is a capability..).

Thoughts?

renoX


More information about the Digitalmars-d-announce mailing list