even more delegate sugar

Oskar Linde oskar.lindeREM at OVEgmail.com
Tue Aug 22 01:00:34 PDT 2006


Don Clugston wrote:
> Tom S wrote:
>> While we're at it, how about allowing the construct:
>> methodName (arg, arg, ..., arg, { ... });
>>
>> to be equivalent to:
>> methodName (arg, arg, ..., arg) { ... }
>>
>>
>> and
>> methodName ({ ... });
>>
>> to
>> methodName {}

Just as I and others have suggested already. I really like it.

>>
>>
>> Then e.g. the 'dotimes' example from 'Lazy Evaluation of Function 
>> Arguments' would become:
>>
>> void foo() {
>>     int x = 0;
>>     dotimes(10) {
>>         writef(x++);
>>     }
>> }
>>
>>
>> Which eliminates the need for lazy evaluation in this case, as it 
>> simply uses a delegate. Moreover, it is more readable and concise at 
>> the same time.
> 
> Sounds nice, but nowhere near enough visual cues.
> If you leave off a semicolon, the meaning completely changes.
>   dotimes(10); {
>    writef(x++);
>  }
> 
>   and
> 
>   dotimes(10)
> 
>   {
>     writef(x++);
>   }
> 
> would both be valid code.

Would they? (assuming there is no dotimes overload with only one 
argument) As I understand Toms suggestion, braces would be required when 
using this syntax. And if not, there is no reason allowing ; to be an 
empty expression in this case, just like

if(10); {
	writef(x++);
}

isn't allowed in D today. The error message is: "use '{ }' for an empty 
statement, not a ';'"

> 
> 
> But an amazing feature of your proposal is that you could write
> a function
> void If(bool b, void delegate (void) f);
> 
> and then write
>   If( cond) { writef(xxxx); }
> 
> which would behave just like the built-in 'if' statement (albeit without 
> an 'else' clause). Interesting.

Indeed.

/Oskar



More information about the Digitalmars-d mailing list