Lazy eval

Walter Bright newshound at digitalmars.com
Mon Aug 21 14:18:04 PDT 2006


Frank Benoit wrote:
> I think the lazy eval is a great feature, but in this form it has also
> great drawbacks.
> 
> The code isn't that much readable as it was before. You don't know what
> will happen. Will that expression be evaluated or not? Or will it be
> evaluated more than once?

It's true there is no clue from the user's side which it is. But there 
also isn't a clue whether the arguments are in, out, or inout. There 
also is no syntactic clue what the function *does*. One must look at the 
function interface and documentation to use it successfully anyway.

It's going to take some caution to use this capability in a productive way.


> There is no possibility to choose between
> 
> func( char[] a ) vs. func( char[] delegate() dg )
> 
> func( funcRetInt() ); vs. func( &funcRetInt );

That's right.

> It would be really important for me to have readable code. I want to
> look at the code, and want to have an impression what will happen.

I hear you, but I'll argue that the onus is on the function designer to 
have a name for the function that clues the user in to what it does. I 
know my example code has functions named "foo" a lot, but such a generic 
meaningless name would be unacceptable for production code.


> What really would help, is if the the delegate is marked in some way as
> such. In the last releases of DMD the {} syntax was there. With it you
> needed the return statement. Perhaps we can choose the {} syntax with an
> optional return statement....
> 
> 
> { "abc"         } => char[] delegate()
> { return "abc"; } => char[] delegate()
> 
> func( "abc" ) calls func( char[] a )
> func({ "abc" }) calls func( char[] delegate() dg )
> func({ return "abc"; }) calls func( char[] delegate() dg )
> 
> With that syntax one can immidiatly see "this is a delegate, if it is
> called or not or more than once depends on func()" and the more typing
> of {} is not too much.

I agree with you that replacing exp with { return exp; } is clear and 
not much additional typing. But I've discovered over and over to my 
surprise that the additional typing causes people to not realize that D 
has that capability. The extra typing simply kills it.



More information about the Digitalmars-d mailing list