A
A J Le Couteur Bisson
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.
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.