foreach & generics syntax proposal

  • Thread starter Wiktor Zychla [C# MVP]
  • Start date
W

Wiktor Zychla [C# MVP]

since generics allow us to implement several IEnumerable<T> interfaces on a
single class, I think that "foreach" should somehow reflect that.

suppose we have a enumerable class

class C : IEnumerable, IEnumerable<int>, IEnumerable<string> { ...

we are allowed to enumerate IEnumerable:

foreach ( object item in o ) ...

but other IEnumerables are not so easy:

foreach ( int item in (IEnumerable<int>)o ) ...

or

class C : ....
{
public IEnumerable<int> Ints
{
get
{
return (IEnumerable<int>)this;
....

foreach ( int item in o.Ints ) ...

What I think of is some form of selecting the IEnumerable with foreach:

foreach ( object item in o ) // enumerate IEnumerable
foreach ( int item in o ) // enumerate IEnumerable
foreach ( string item in o ) // enumerate IEnumerable
foreach<int> ( int item in o ) // enumerate IEnumerable<int>
foreach<string> ( string item in o ) // enumerate IEnumerable<string>

since the type name is duplicated in former cases, I would even like to
write:

....
foreach<int> ( item in o ) ... // enumerate IEnumerable<int>
foreach<string> ( item in o ) ... // enumerate IEnumerable<string>

and that's it. I open the discussion.

Regards,
Wiktor Zychla
 
N

Nick Hounsome

Wiktor Zychla said:
since generics allow us to implement several IEnumerable<T> interfaces on
a single class, I think that "foreach" should somehow reflect that.

Implementing IEnumerable<int> should mean that your class IS-A sequence of
int which means that it is also a sequence of object but it cannot logically
suppose we have a enumerable class

class C : IEnumerable, IEnumerable<int>, IEnumerable<string> { ...

we are allowed to enumerate IEnumerable:

foreach ( object item in o ) ...

but other IEnumerables are not so easy:

foreach ( int item in (IEnumerable<int>)o ) ...

or

class C : ....
{
public IEnumerable<int> Ints
{
get
{
return (IEnumerable<int>)this;

Now there IS a valid use for enumerable properties or methods because then
the class HAS-A sequence of whatever.
The text book example (although not with different types) is:

class MyTreeNode
{

// enumerate nodes in PreOrder
public IEnumerable<MyTreeNode> PreOrder()
{
....
yield return n; // these things are really easy in 2.0 - in
1.1 it is too much like work.
....
}
public IEnumerable<MyTreeNode> InOrder()
{
....
}
public IEnumerable<MyTreeNode> PostOrder()
{
....
}
public IEnumerable<MyTreeNode> Children
{
...
}
}

I tend to feel that methods are more appropriate than properties for this
sort of stuff however you could see how something like node.Children and
person.Parents are more naturally a properties.
 
G

Guest

Well wouldn't that allow a logic error like:

foreach<int> ( string item in o )

Why make the user type the type twice? Why make the user need to know that
the object is generic?

And let's say that you pass a collection of widgets to a client, the
client's method need only accept an IEnumerable and foreach across it,
accessing the widgets. He needn't know the structure of the collection any
more than that. If you then make your collection generic, will he need to
change his code to reflect that?
 
M

Mattias Sjögren

Wiktor,

Frankly I don't see
foreach<int> ( int item in o ) // enumerate IEnumerable<int>

or even
foreach<int> ( item in o ) ... // enumerate IEnumerable<int>

as a significant improvement over
foreach ( int item in (IEnumerable<int>)o ) ...

This last one doesn't require that much more typing, and it's pretty
clear what's happening. So I don't think it's work introducing new
syntax for this.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top