foreach casts

  • Thread starter Thread starter A J Le Couteur Bisson
  • Start date Start date
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.
 
A J Le Couteur Bisson said:
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.

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

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(IEnumerable collection, Type t, FilterMethod
filterMethod) { ... }

and use it as:

foreach(Thing t in FilterByType(collection, typeof(Thing),
FilterMethod.SkipNonMatching) // For case (b)
{
}

Stu
 
Back
Top