473,738 Members | 11,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Feature Request] "first:" "last:" sections in a "foreach" block

I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm posting
it here to gather input to refine it, in an "open Source" manner, and in an
attempt to build a ground-swell of support to convince the folks at
Microsoft to add it.
Proposal: "first:" "last:" sections in a "foreach" block

The problem:
The foreach statement allows iterating over all the elements of a
collection. However, often the first or last element of the collection must
be handled differently than the others in the collection. For example, if
we were generating a list of items for display, we'd want a comma after
every item except the last. In these cases, foreach cannot be used, and the
alternates which can be used are generally ugly and often less efficient
than foreach. An elegant solution keeping with the concept of C# is needed.

The proposal:
I suggest adding four new keywords (only available inside foreach block) to
direct the compiler to our exact intentions. "first:" "last:" "other:"
"all:"

Example:
foreach (Person p in personCollectio n)
{
first:
Console.WriteLi ne("Names: {0},", p.Name");
other:
Console.WriteLi ne(" {0},", p.Name");
last:
Console.WriteLi ne(" {0}", p.Name");
}
A block not preceeded by one of those keywords would be assumed to be an
"all:" block, exact as is the case now.

Analysis:
The advantages of the feature:
It solves a problem in an elegant way, keeping with the design concepts of
C#.
It is handled entirely by the compiler, so that it has ZERO effect on code
that does not use it, and it requires no changes to the class framework or
the CLR. (These were problems with other suggested solutions for this)

The disadvantages:
It requires four keywords, although this could be reduced to three
(eliminating "all:") and, they would each be very context sensitive, so the
conflicts with user-defined identifiers is unlikely.

Implementation:
Presently, a foreach construct is largely "syntactic sugar" around a while
loop using an IEnumerator, such that code written as
foreach (int i in arry)
{
Console.WriteLi ne(i);
}

Would be compiled much as if it were written:
IEnumerator n = arry.GetEnumera tor();
while(n.MoveNex t())
{
int i = (int) n.Current;
Console.WriteLi ne(i);
}

Similarly, code using these blocks would also have a direct translation:
foreach (int i in arry)
{
First:
Console.WriteLi ne("{0} - first", i);
Others:
Console.WriteLi ne(i);
}

Would be compiled as:
IEnumerator n = arry.GetEnumera tor();
if (n.MoveNext())
{
int i = (int) n.Current;
Console.WriteLi ne("{0}-- first ", i);
while(n.MoveNex t())
{
i = (int) n.Current;
Console.WriteLi ne(i);
}
}
(The internal representation of a "last:" block is a bit more involved, but
still basically straightforward )

Any comments are suggestion would be appreciated.

Truth,
James Curran
MVP (for .um. VC++)

Nov 16 '05 #1
32 4150
"James Curran" <Ja*********@mv ps.org> wrote:
[...]
The foreach statement allows iterating over all the
elements of a collection. However, often the first
or last element of the collection must be handled
differently than the others in the collection.
[...]
Presently, a foreach construct is largely "syntactic
sugar" around a while loop using an IEnumerator
[...]
I'd say that your proposal is rather unpleasant-looking "syntactic sugar" in
itself.

'foreach' and indexed 'for' are pretty much interchangeable . Using a 'for'
loop in those special cases and checking for specific iterations is hardly a
cumbersome task, certainly not enough so to merit a new keyword in the
language. What if it's the *second* iteration you want to treat differently;
should there be a 'second' keyword as well, or some kind of 'foreach ...
case' construct?
Any comments are suggestion would be appreciated.


I just don't think it's necessary.

I'm somewhat influenced by the fact that I tend to use 'foreach' when I want
to iterate over all of the elements but don't care which order they come up
in. When I want things in a definite order, I use 'for'. (Is IEnumerator
supposed to return things in any particular order? I don't know.)

P.
Nov 16 '05 #2

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:c4******** **@hercules.bti nternet.com...
'foreach' and indexed 'for' are pretty much interchangeable .
Actually they aren't. foreach works for every class that implements
IEnumerable, which is small and easy to implement. Indexing requires
implementing IList which is more complicated. Many collection
implementations don't lend themselves well to indexing (anything which using
a tree instead of an array behind the scenes). Check out their desciption
pages in the MSDN. In the framework over twice as many classes implement
IEnumerable as IList.
I'm somewhat influenced by the fact that I tend to use 'foreach' when I

want
to iterate over all of the elements but don't care which order they come up
in <<

Just because there is no particular order to the items in the collection,
does not mean when being processed, the first and last do not need to be
processed differently. As in my example of adding the comma, often it's just
the fact that this item is the first processed is enough to require special
processing.
What if it's the *second* iteration you want to treat differently;
should there be a 'second' keyword as well, or some kind of 'foreach ...
case' construct?


No. Needing to treat the first or last item differently is fairly common.
Needing to treat an item besides the first or last differently is much
rarer. Also, there is no simple implementation for treating a middle item
differently.

I guess the basic question is what is the point of having a foreach in the
language anyway, when it can always be replaced by a for or while statement?
If you see an advantage in having it in the language, you should see the
advantage of this.
Nov 16 '05 #3

"James Curran" <Ja*********@mv ps.org> wrote in message
news:OE******** ******@TK2MSFTN GP12.phx.gbl...
I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm
posting
it here to gather input to refine it, in an "open Source" manner, and in
an
attempt to build a ground-swell of support to convince the folks at
Microsoft to add it.
Proposal: "first:" "last:" sections in a "foreach" block

The problem:
The foreach statement allows iterating over all the elements of a
collection. However, often the first or last element of the collection
must
be handled differently than the others in the collection. For example, if
we were generating a list of items for display, we'd want a comma after
every item except the last. In these cases, foreach cannot be used, and
the
alternates which can be used are generally ugly and often less efficient
than foreach. An elegant solution keeping with the concept of C# is
needed.

Ok, I agree that this is a situation where no elegant solution exists. I've
seen a few other solutions that would help with this(implicit index and
length variables, my own named enumerators with an extended IEnumerator) but
as you illustrate they aren't easy or possible without modifying base
classes.. The proposal:
I suggest adding four new keywords (only available inside foreach block)
to
direct the compiler to our exact intentions. "first:" "last:" "other:"
"all:"
While this is interesting, I have to agree with Paul that it doesn't catch
edge cases, which is something that needs to be addressed. I'll continue to
think about it and see what I can think of to manage such. There is
potential utility in this(although it may not be a feature that would make
the mainstream compiler) and it is atleast worth exploring.
The disadvantages:
It requires four keywords, although this could be reduced to three
(eliminating "all:") and, they would each be very context sensitive, so
the
conflicts with user-defined identifiers is unlikely.
all could either be implicit and\or you could piggy back with default.
Additionally using case <label>:(or another word) and treating a foreach
like a switch would allow you to modify the syntax without disrupting
existing code. Two word keywords are syntactically possible(as evidenced
with yield in C# 2.0) while it isn't legal to have a two word identifier.
Implementation:
Presently, a foreach construct is largely "syntactic sugar" around a while
loop using an IEnumerator, such that code written as
foreach (int i in arry)
{
Console.WriteLi ne(i);
}

Would be compiled much as if it were written:
IEnumerator n = arry.GetEnumera tor();
while(n.MoveNex t())
{
int i = (int) n.Current;
Console.WriteLi ne(i);
}

Similarly, code using these blocks would also have a direct translation:
foreach (int i in arry)
{
First:
Console.WriteLi ne("{0} - first", i);
Others:
Console.WriteLi ne(i);
}

Would be compiled as:
IEnumerator n = arry.GetEnumera tor();
if (n.MoveNext())
{
int i = (int) n.Current;
Console.WriteLi ne("{0}-- first ", i);
while(n.MoveNex t())
{
i = (int) n.Current;
Console.WriteLi ne(i);
}
}
(The internal representation of a "last:" block is a bit more involved,
but
still basically straightforward )

Any comments are suggestion would be appreciated.
I think that for features like this the best way to explore the usage is to
actually use them. Have you considered modifying mono or rotor and adding
the feature(or permutations of it)? It will give you, and others if you
release it, a chance to see how it would really work in real code.
Truth,
James Curran
MVP (for .um. VC++)

Nov 16 '05 #4

"Paul E Collins" <fi************ ******@CL4.org> wrote in message
news:c4******** **@hercules.bti nternet.com...
"James Curran" <Ja*********@mv ps.org> wrote:
[...]
The foreach statement allows iterating over all the
elements of a collection. However, often the first
or last element of the collection must be handled
differently than the others in the collection.
[...]
Presently, a foreach construct is largely "syntactic
sugar" around a while loop using an IEnumerator
[...]
I'd say that your proposal is rather unpleasant-looking "syntactic sugar"
in
itself.

'foreach' and indexed 'for' are pretty much interchangeable . Using a 'for'
loop in those special cases and checking for specific iterations is hardly
a
cumbersome task, certainly not enough so to merit a new keyword in the
language. What if it's the *second* iteration you want to treat
differently;
should there be a 'second' keyword as well, or some kind of 'foreach ...
case' construct?


I prefer case or another context sensitive keyword myself. case first;case
last;case index 2; case index 5;case index default;case all may work and
wouldn't cause existing code to fail.
Any comments are suggestion would be appreciated.
I just don't think it's necessary.

I'm somewhat influenced by the fact that I tend to use 'foreach' when I
want
to iterate over all of the elements but don't care which order they come
up
in. When I want things in a definite order, I use 'for'. (Is IEnumerator
supposed to return things in any particular order? I don't know.)


Not defined by any standard, atleast globablly. An enumerator returns
objects in whatever order it sees fit. A given implementation of an
IEnumerator can specify that it returns objects that are sorted, or the
order they are stored in the IList, or reversed or what not, but there is no
explicit standard.
P.

Nov 16 '05 #5
> The foreach statement allows iterating over all the elements of a
collection. However, often the first or last element of the collection must be handled differently than the others in the collection. For example, if
we were generating a list of items for display, we'd want a comma after
every item except the last. In these cases, foreach cannot be used, and the alternates which can be used are generally ugly and often less efficient
than foreach.


In that case, you should use the Enumerator directly with a for loop which
gives you full control over the enumeration.

e.MoveNext();
DosomethingWith FirstElement(e. Current);

while(e.MoveNex t())
{
Object o = e.Current;
}

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #6
James Curran wrote:
I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm posting
it here to gather input to refine it, in an "open Source" manner, and in an
attempt to build a ground-swell of support to convince the folks at
Microsoft to add it.
Proposal: "first:" "last:" sections in a "foreach" block

[...]


This isn't a bad idea at all, in my opinion. As you say, one must
appreciate 'foreach' to appreciate this idea.

I've got a feeling that people in general might prefer something like
the following, though:

foreach (Person p in personCollectio n)
{
first
{
Console.WriteLi ne("Names: {0},", p.Name");
}
other
{
Console.WriteLi ne(" {0},", p.Name");
}
last
{
Console.WriteLi ne(" {0}", p.Name");
}
}
Nov 16 '05 #7
Paul E Collins <fi************ ******@CL4.org> wrote:

<snip - I'm still mulling over the suggestion itself>
I'm somewhat influenced by the fact that I tend to use 'foreach' when I want
to iterate over all of the elements but don't care which order they come up
in. When I want things in a definite order, I use 'for'. (Is IEnumerator
supposed to return things in any particular order? I don't know.)


IEnumerator's GetEnumerator method specifies:

<quote>
Enumerators only allow reading the data in the collection.
....
Initially, the enumerator is positioned before the first element in the
collection.
....
Current returns the same object until either MoveNext or Reset is
called. MoveNext sets Current to the next element.
</quote>

That would suggest to me that if the collection *has* a natural order,
the enumerator should return elements in that order. I certainly have
no compunction with relying on foreach to iterate through an IList in
the natural way.

It's when the collection *doesn't* have an obvious natural order (e.g.
for Hashtables) that I wouldn't want to rely on the ordering.

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

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Paul E Collins <fi************ ******@CL4.org> wrote:

<snip - I'm still mulling over the suggestion itself>
I'm somewhat influenced by the fact that I tend to use 'foreach' when I
want
to iterate over all of the elements but don't care which order they come
up
in. When I want things in a definite order, I use 'for'. (Is IEnumerator
supposed to return things in any particular order? I don't know.)
IEnumerator's GetEnumerator method specifies:

<quote>
Enumerators only allow reading the data in the collection.
...
Initially, the enumerator is positioned before the first element in the
collection.
...
Current returns the same object until either MoveNext or Reset is
called. MoveNext sets Current to the next element.
</quote>


Thats IEnumerable actually. While IEnumerator has the same suggestion, I
think its a touch irresponsible to define an IEnumerator as working in any
order. The semantics of IEnumerable, espeically when working with IList,
suggest to me that natural order should be used when available and no
documentation says otherwise, but a given IEnumerator should not be
constrained in its design by any specification(A lthough that is pretty moot
considering foreach can't handle IEnumerator directly, unfortunatly).

Its picky, I know, but I consider the difference between the two an
important point that MSDN and C# to an extent doesn't really illustrate
well. That would suggest to me that if the collection *has* a natural order,
the enumerator should return elements in that order. I certainly have
no compunction with relying on foreach to iterate through an IList in
the natural way.

It's when the collection *doesn't* have an obvious natural order (e.g.
for Hashtables) that I wouldn't want to rely on the ordering.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
James Curran wrote:
The problem:
The foreach statement allows iterating over all the elements of a
collection. However, often the first or last element of the collection must
be handled differently than the others in the collection. For example, if
we were generating a list of items for display, we'd want a comma after
every item except the last. In these cases, foreach cannot be used, and the
alternates which can be used are generally ugly and often less efficient
than foreach. An elegant solution keeping with the concept of C# is needed.


I don't disagree that there could be some potential benefit.

However, in the case that you state, it is relatively simple to handle this
case:

String output = "Names: ";
foreach (String name in names)
{
output += name + ",";
}
output.TrimEnd( ",");
Console.WriteLi ne(output);

Which in my opinion is pretty readable.

I think that the biggest problem is the last: case. During enumeration, the
last element isn't determinable until you are past the end, and therefore would
impose more requirements on collections.
Nov 16 '05 #10

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

Similar topics

32
578
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.