lazy redux

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Dec 7 12:47:28 PST 2009


Leandro Lucarella wrote:
> Michal Minich, el  7 de diciembre a las 13:51 me escribiste:
>> Hello Denis,
>>
>>>> 1. auto t = new Thread ( { a=42 } );
>>>> or auto t = new Thread ( () { a=42 } );
>>> It already works, just try it (but don't forget to put a semicolon at
>>> the  end).
>>>
>>>> 2. array.findAll (arr, (item) { item.contains ("abc") } ); 3. foo (
>>>> (a, b) { a + b } );
>>>>
>>>> 4. array.findAll (arr, (item) { return item.contains ("abc"); } );
>>>>
>>>> I'm not proposing this syntax (maybe I probably should, but I have
>>>> feeling I would not be first). It may not even be possible to parse
>>>> it,  but seems to me more similar to how currently functions are
>>>> written. In  this setting {exp} or {stm} is not *special* case.
>>>>
>>> I believe it would work. And yes, it was already proposed by many
>>> others.
>>>
>> it works with two differences:
>>
>> 1. the semicolon is required, even if the body consist only of one
>> expression. This is a minor detail.
>> 2. more importantly - parameter types must be specified explicitly.
>> I don't understand why type of b cannot be inferred in this example:
>>
>> void foo (void delegate (int a) dg)
>> {
>>    dg(1);
>> }
>>
>> void main ()
>> {
>>    foo ( (int b)  { writeln (b); } );
>> }
> 
> It doesn't do implicit returning either:
> 
> $ cat -n x.d
>      1	
>      2	import std.stdio;
>      3	
>      4	void foo (int delegate() dg)
>      5	{
>      6		writeln(dg());
>      7	}
>      8	
>      9	void main()
>     10	{
>     11		int a = 5;
>     12		foo({a;});
>     13	}
>     14	
> $ dmd x.d
> x.d(12): Error: var has no effect in expression (a)
> x.d(12): Error: function x.foo (int delegate() dg) is not callable using argument types (void delegate())
> x.d(12): Error: cannot implicitly convert expression (__dgliteral1) of type void delegate() to int delegate()
> 
> To make it work you have to do it like this:
>     12		foo({return a;});
> 
> Which is considerably uglier than
>     12		foo({a});
> 
> At least when talking about replacing "lazy" :)
> 
> Nobody wants to write:
> enforce({ return a == 5; });
> instead of:
> enforce(a == 5);
> 
> But:
> enforce({a == 5});
> 
> Could be acceptable.

A nit - it's the second argument of enforce that must be lazy, e.g.

enforce(a == 5, {return text("a is not 5, it's ", a);});

Not looking good anyway.


Andrei



More information about the Digitalmars-d mailing list