foreach & generics syntax proposal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wiktor Zychla [C# MVP]

    foreach & generics syntax proposal

    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<str ing> { ...

    we are allowed to enumerate IEnumerable:

    foreach ( object item in o ) ...

    but other IEnumerables are not so easy:

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

    or

    class C : ....
    {
    public IEnumerable<int > Ints
    {
    get
    {
    return (IEnumerable<in t>)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<str ing>

    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<str ing>

    and that's it. I open the discussion.

    Regards,
    Wiktor Zychla

  • Nick Hounsome

    #2
    Re: foreach &amp; generics syntax proposal


    "Wiktor Zychla [C# MVP]" <wzychla@nospm. ii.uni.wroc.pl. nospm> wrote in
    message news:u%23aNrR%2 3UGHA.5900@tk2m sftngp13.phx.gb l...[color=blue]
    > since generics allow us to implement several IEnumerable<T> interfaces on
    > a single class, I think that "foreach" should somehow reflect that.[/color]

    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
    also be a sequence of string hence implementing IEnumerable<str ing> is
    logically wrong.
    [color=blue]
    > suppose we have a enumerable class
    >
    > class C : IEnumerable, IEnumerable<int >, IEnumerable<str ing> { ...
    >
    > we are allowed to enumerate IEnumerable:
    >
    > foreach ( object item in o ) ...
    >
    > but other IEnumerables are not so easy:
    >
    > foreach ( int item in (IEnumerable<in t>)o ) ...
    >
    > or
    >
    > class C : ....
    > {
    > public IEnumerable<int > Ints
    > {
    > get
    > {
    > return (IEnumerable<in t>)this;[/color]

    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<MyT reeNode> PreOrder()
    {
    ....
    yield return n; // these things are really easy in 2.0 - in
    1.1 it is too much like work.
    ....
    }
    public IEnumerable<MyT reeNode> InOrder()
    {
    ....
    }
    public IEnumerable<MyT reeNode> PostOrder()
    {
    ....
    }
    public IEnumerable<MyT reeNode> 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.


    Comment

    • PIEBALD

      #3
      Re: foreach &amp; generics syntax proposal

      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?

      Comment

      • Mattias Sjögren

        #4
        Re: foreach &amp; generics syntax proposal

        Wiktor,

        Frankly I don't see
        [color=blue]
        >foreach<int> ( int item in o ) // enumerate IEnumerable<int >[/color]

        or even
        [color=blue]
        >foreach<int> ( item in o ) ... // enumerate IEnumerable<int >[/color]

        as a significant improvement over
        [color=blue]
        >foreach ( int item in (IEnumerable<in t>)o ) ...[/color]

        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

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...