C#5 desiderata
bearophile
bearophileHUGS at lycos.com
Fri May 28 15:40:28 PDT 2010
You can remember when I have shown this link, about desired features for C#4:
http://stackoverflow.com/questions/138367/
Instead of many of those features, C# devs have chosen to add only very few ones, but rather basic ones, like the dynamic attribute, that change some how C# programs can be written.
Recently a similar question has appeared about C#5 (probably some answers are repeated from the precedent thread):
http://stackoverflow.com/questions/2875533/
Some of those things are meaningful only in the context of C#4.
Note: what follows below is not my list of desiderata for D2/D3, it's just a summarized version of that that stackoverflow thread about C#5 desiderata.
-------------------
Return tuples:
public (int, string, double) Foo() {
return (42, "foo", 4.2);
return {"Hello World", 42, 4.2};
}
// elsewhere: item1 is a string, item2 is an int, item3 is a double.
var (item1, item2, item3) = Foo();
var {item1, item2, item3} = Foo();
var myTuple = (42, "foo", 4.2);
var myTuple = {42, "foo", 4.2};
===
Tuple<int, string, double> myTuple = Tuple.Create(42, "foo", 4.2);
-------------------
in for arrays:
if (x in (1, 2, 3)) {}
-------------------
Automatic Flag Enums (automatically enumerate in powers of two).
-------------------
Null Safe Member Operator:
Instead of doing:
var obj = Foo();
Bar value = null;
if (obj.Bar != null && obj.Bar.Something != null)
value = obj.Bar.Something.DoSomething();
You can do this with Groovy's null safe member operator:
var obj = Foo();
var value = obj?.Bar?.Something?.DoSomething();
-------------------
Nested Iterators:
public override IEnumerable<int> Foo() {
yield return 1;
yield return 2;
yield return base.Foo();
// yield foreach base.Foo();
}
Instead of:
public override IEnumerable<int> Foo() {
yield return 1;
yield return 2;
foreach(var i in base.Foo()) yield i;
}
As explained here too:
http://research.microsoft.com/en-us/projects/specsharp/iterators.pdf
-------------------
Weak delegates, to make it easy to implement weak events. A modifier like ~ could mark a normal delegate weak:
public event EventHandler StrongEvent;
public event EventHandler~ WeakEvent; // Shortcut for WeakDelegate<EventHandler>
-------------------
Arrays indexed by enums, delphi style:
enum Weekdays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
private int[Weekdays] dayFun = new [] { 0, 0, 1, 1, 3, 10, 8 };
// later..
mondayFun = dayFun[Weekdays.Monday];
-------------------
Exception grouping to avoid duplicating the same handling logic
try
{
}
catch (ArgumentOutOfRangeException)
catch (ArgumentNullException)
{
// Catch a ArgumentOutOfRangeException or a ArgumentNullException
}
(I think this syntax can be improved, just using the comma).
-------------------
@memoize
Or something similar user-defined. It is really useful, to avoid coding true dynamic programming algorithms.
-------------------
"as" operator for objects (This is already present in C#4).
Bye,
bearophile
More information about the Digitalmars-d
mailing list