Denis Koroskin wrote:
On Thu, 05 Feb 2009 12:32:15 +0300, Kagamin <s...@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; }

What if you wanted to just execute one expression and return void? This is relevant when e.g. large objects are involved that shouldn't be copied unwittingly.

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; } );

Aha! So here you are using a void-returning function. Now what if there was another overload of foo in place:

void foo(int delegate(ref int i) inc);

Which foo is to be called? The one that infers a return type of int or the one that assumes the code just returns void?

I can put this enhancement request into bugzilla if anyone likes it.

It would be great to add the parameter type deduction stuff; that is already talked about and doesn't seem to have many issues. It does have one, which I'm sure people here will see rather quickly.


Andrei

Reply via email to