Lambda syntax, etc
Denis Koroskin
2korden at gmail.com
Thu Feb 5 05:17:50 PST 2009
On Thu, 05 Feb 2009 12:32:15 +0300, Kagamin <spam at here.lot> wrote:
> bearophile Wrote:
>
>> C#2 has lambdas, and C#3 adds closures and more type inferencing, so
>> C#3+ supports the following syntaxes:
>> (int i) => { return i % 3 == 1; } // C#2
>> i => i % 3 == 1 // C#3
>> i => { return i % 3 == 1; } // C#3, with statements too
>> To define a delegate o delegate closure:
>> Func<int> foo = i => { return i % 3 == 1; };
>> Func<int> foo = i => i % 3 == 1;
>> Func<int> bar = () => 2;
>> But this isn't allowed:
>> Func<void> bar = () => 2;
>
> Yeah, C# lambdas are the killer feature. Slick, readable, C-compatible.
> Anders knows his job. Let's face it: delegate literals suck a little,
> mixins as delegates suck a lot, the former is too verbose, the latter
> just sucks.
I don't like C# lambda syntax (although it is not half as bad as C++ lambda syntax).
I believe D delegate syntax is superior due to its natural and unambiguous syntax.
But yes, it could be made shorter by improving type deduction:
int delegate(int) inc = (i) { i + 1; }
Which would be the same as
int delegate(int) inc = (int i) { return i + 1; }
where i's type is deduced from inc's type and the only expression (i + 1) made a return value:
auto x = inc(5); // yields 6
Here is an another example:
void foo(void delegate(ref int i) inc);
Could be used as follows:
foo( (i) { ++i; } );
as opposed to
foo( (ref int i) { ++i; } );
I can put this enhancement request into bugzilla if anyone likes it.
More information about the Digitalmars-d
mailing list