But... I don't want my delegates to be lazy - breeding advice

Derek Parnell derek at nomail.afraid.org
Thu Aug 24 00:16:18 PDT 2006


On Wed, 23 Aug 2006 22:54:35 -0700, Walter Bright wrote:

> Ok, I've thought about it a bit:
> 
> 	void foo(int x);	// same as before 0.165
> 	void foo(int delegate() x)	// same as before 0.165
> 
> and now:
> 
> 	void foo(lazy int x);
> 
> In other words, 'lazy' is now a parameter storage class. This means that:
> 
> 	void foo(int x);
> 	void foo(lazy int x);
> 
> cannot be distinguished based on overloading, but:
> 
> 	void foo(lazy int x);
> 	void foo(int delegate() x);
> 
> can be. The implicit conversion of a value to a delegate returning that 
> value would be removed. The conversion happens always (not implicitly) 
> if the parameter storage class is 'lazy'.

Thanks for do this Walter. The idea of converting expressions to delegates
is a good one, and with these changes proposed here it removes a lot of the
'gotchas' that come with implicit conversions. (uint --> int --> uint, and
long --> int --> short are still sore points, BTW).

I also assume that the mutability of lazy parameters is identical to the
'in' parameter storage class. That is, any changes to the actual value
passed to the function are never passed back to the caller, but that
doesn't stop one modifying data passed as references.

 import std.stdio;
 void foo(lazy char[] x)
 {
    x[0..2] = x[1..2] ~ x[0..1];
    x ~= "k";
    writefln("%s", x); // --> "back";
 }

 void main()
 {
    char[] d = "abc";
    foo( d );
    writefln("%s", d); // --> "bac";

 }
       
 
-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
24/08/2006 4:45:03 PM



More information about the Digitalmars-d mailing list