foreach casts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A J Le Couteur Bisson

    #1

    foreach casts

    There seems to be a missed opportunity in the implementation of foreach.
    Presently the type specified in the foreach must match the type of every
    element in the collection. Both of the following alternative
    implementations provide additional functionality without breaking existing
    code.

    a) foreach(thing t in collection) // cast operates like 'as'
    {
    if(t != null) // t is a thing
    t.thingmethod() // for example
    }

    b) foreach(thing t in collection) // cast excludes non matching types
    {
    // only things get this far (non-things are skipped)
    t.thingmethod() // for example
    }

    Version b seems the most useful. Since all existing code must ensure that
    collection contains only things this would be a strict extension of the
    present behaviour rather than a change.

    Andy.


  • Jon Skeet [C# MVP]

    #2
    Re: foreach casts

    A J Le Couteur Bisson <noone@nowhere. com> wrote:[color=blue]
    > There seems to be a missed opportunity in the implementation of
    > foreach. Presently the type specified in the foreach must match the
    > type of every element in the collection. Both of the following
    > alternative implementations provide additional functionality without
    > breaking existing code.
    >
    > a) foreach(thing t in collection) // cast operates like 'as'
    > {
    > if(t != null) // t is a thing
    > t.thingmethod() // for example
    > }
    >
    > b) foreach(thing t in collection) // cast excludes non matching types
    > {
    > // only things get this far (non-things are skipped)
    > t.thingmethod() // for example
    > }
    >
    > Version b seems the most useful. Since all existing code must ensure
    > that collection contains only things this would be a strict extension
    > of the present behaviour rather than a change.[/color]

    No, it would be a change - code which currently throws an exception
    then wouldn't. Code which currently rejects invalid collections passed
    to it would stop doing so, for instance. It's definitely a breaking
    change.

    I can see that it would be useful in some situations, but I think the
    current behaviour is useful in others.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Stu Smith

      #3
      Re: foreach casts


      "A J Le Couteur Bisson" <noone@nowhere. com> wrote in message
      news:ce8hhv$1er $1@newsfeed.th. ifl.net...[color=blue]
      > There seems to be a missed opportunity in the implementation of foreach.
      > Presently the type specified in the foreach must match the type of every
      > element in the collection. Both of the following alternative
      > implementations provide additional functionality without breaking existing
      > code.
      >
      > a) foreach(thing t in collection) // cast operates like 'as'
      > {
      > if(t != null) // t is a thing
      > t.thingmethod() // for example
      > }
      >
      > b) foreach(thing t in collection) // cast excludes non matching types
      > {
      > // only things get this far (non-things are skipped)
      > t.thingmethod() // for example
      > }
      >
      > Version b seems the most useful. Since all existing code must ensure that
      > collection contains only things this would be a strict extension of the
      > present behaviour rather than a change.[/color]

      I think this is the sort of functionality which could be better handled by a
      library function or class rather than a language change. You could write
      something like:

      IEnumerable FilterByType(IE numerable collection, Type t, FilterMethod
      filterMethod) { ... }

      and use it as:

      foreach(Thing t in FilterByType(co llection, typeof(Thing),
      FilterMethod.Sk ipNonMatching) // For case (b)
      {
      }

      Stu
      [color=blue]
      >
      > Andy.
      >
      >[/color]


      Comment

      Working...