473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.
Jul 21 '05 #1
2 1341
A J Le Couteur Bisson <no***@nowhere.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

"A J Le Couteur Bisson" <no***@nowhere.com> wrote in message
news:ce**********@newsfeed.th.ifl.net...
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

Andy.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Howard | last post by:
Hi, I am maintaining a lot of code that is rife with C-style casts. I've seen a lot of comments that one should not use C-style casts at all. But I'm wondering what harm there could be in...
31
by: Jacob | last post by:
It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e....
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
2
by: A J Le Couteur Bisson | last post by:
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...
10
by: Ralf | last post by:
Regarding numerical types, in my view, casts fall in one of two categories: 1. Casts that change the value of an object 2. Casts that are actually redundant, but get rid of compiler/lint warnings...
13
by: dpbsmith.janissary.2006 | last post by:
I know C++ mostly from "learning by doing." My main reference is Stoustrup's book. I was puzzled by something in a colleague's code that looked like this: abc=BOOL(def) I asked him what that...
5
by: james | last post by:
Hey Guys, Would anyone mind explaining why a foreach will implicitly do unsafe casts, and if there is a way to turn this off? Here is an example: ulong vals = new ulong { ulong.MaxValue };...
81
by: jacob navia | last post by:
Hi I am still writing my tutorial book about C. Here is the section about casts. I would be interested in your opinions about this. Some people have definite views about this subject ("never...
61
by: jacob navia | last post by:
Continuing the discussion about casts, I would like to know your opinions about the hairy subject of casts as lvalues, i.e. This will fail under lcc-win32, but MSVC and gcc will accept it. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.