Home Dashboard Directory Help
Search

Syntactic sugar for IEnumerable<T> by Pavel Minaev [MSFT]


Status: 

Closed
 as Won't Fix Help for as Won't Fix


5
2
Sign in
to vote
Type: Suggestion
ID: 375929
Opened: 10/16/2008 1:17:12 AM
Access Restriction: Public
0
Workaround(s)
view

Description

IEnumerable<T> has a long history of being a "special" type in C#, starting with special handling in "foreach" and "yield". It is even more important with the introduction of LINQ, as all LINQ to Objects and XML operations are centered around IEnumerable<T>. Today, on a codebase that uses .NET 3.5 heavily, I find that there are more mentions of "IEnumerable" in my code than of all other collections (including arrays) combined, and more than references to "object". In my opinion, it is a sure sign some syntactic sugar for IEnumerable<T>, and some fundamental operations, is due. I propose the following:


1. A shortcut syntax for IEnumerable<T>. One suggestion would be "T...", e.g.:

    int... xs = { 1, 2, 3 };
    int x = xs.First();
    
    An asterisk (*) would probably be better, but it's already used for pointers. Or it could be a new short keyword - maybe steal "seq" from F#?

    seq<int> xs = { 1, 2, 3 }

    Either way, "IEnumerable" is just too long to spell all the time. It would also be useful if (when?) you introduce inline iterators: "new seq { ... }" is so much shorter than "new IEnumerator<int> { ... }", and allows for type inference.


2. Lift some most commonly used methods from Enumerable as operators: ElementAt() as operator[], Concat() as binary operator+, etc. I.e.:

IEnumerable<int> xs = { 1, 2, 3 }, ys = { 4, 5, 6 }, zs;
...
zs = xs + ys; // same as "Enumerable.Concat(xs, ys)"
xs += ys; // same as "xs = xs.Concat(xs, ys)"
z = zs[0]; // same as "zs.ElementAt(0)"

A few others that could be similarly treated are cartesian join (as multiplication), and maybe think something up for Except/Intersect/Union. Though in practice, I would be quite content with just Concat() as operator+ - it seems to be the one that's actually used most often.
Details
Sign in to post a comment.
Posted by lostmsu on 2/2/2011 at 7:48 AM
Why not use new[] { 1, 2, 3 }?
Posted by Microsoft on 7/21/2010 at 9:54 AM
I like the postfix squiggle! It takes a little getting used to, but has nice composability properties. It gets a little wavy when you start piling them up (int~~~), but hey arrays get boxy too (int[][][]).

I'll definitely bring this up the next time we revisit the feature!

Thanks again,

Mads
Posted by Pavel Minaev [MSFT] on 4/28/2010 at 12:32 PM
To further extend on this, I would like to propose the following specific syntax:

int~ xs; // same as IEnmerable<int>

The idea is that "~" is mnemonic, and represents a stream (of values).

This proposal is in line with existing curly braces language family practice of using single-char modifiers (e.g. * and &), but at the same time it is a distinct modifier that is, to my knowledge, not used for any purpose in any other related language, thus removing possibility of confusion. Also, since ~ is not an existing binary operator, this is unambiguously a type declaration rather than expression.
Posted by Microsoft on 1/15/2009 at 3:47 PM
Thank you for your suggestion!

We are at a point in the product cycle where we can no longer add new features to the next release, so I'll resolve this as won't fix.

The idea of abbreviating IEnumerable<T> is already on our (long!) list of potential features for future releases. I agree that it is very ferquently used, and we want to encourage people to use it even more.

Great idea to also overload the operators - there'd be some details to work out, but that's what we're here for. I've added this to our future ideas list.

Thanks again,

Mads Torgersen, C# Language PM
Posted by THERAOT on 11/2/2008 at 4:23 PM
I think not every one would like it (hey, I like it!), but couldn't they be extension operators? (I mean, let's pry to dad Microsoft to get extension operator, and you'll be able to write this away).

Why ask for simple sugar when you can have all the sugar canes?
what? performance, ok, ok, ask for inline optimization too, most of those extensions will be single line anyway.

of course the "int... xs = { 1, 2, 3 };" idea is clever, It will not hurt me, if we get it ^_^
Sign in to post a workaround.