www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - shorter foreach syntax - C++0x range-based for

reply Nick Treleaven <nospam example.net> writes:
There's a C++0x proposal for a range-based 'for' statement:
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html

The upcoming GCC 4.6 C++ compiler changes list support for this:
http://gcc.gnu.org/gcc-4.6/changes.html

I think the syntax could be useful for D to shorten and improve on the 
status quo a little. Here's the C++ example:

int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
  x *= 2;

Currently D has:

int array[5] = [1, 2, 3, 4, 5];
foreach (ref x; array)
  x *= 2;

I think this is better:

for (ref x : array)
  x *= 2;

Apart from being 4 chars shorter, I think it looks more natural using the 
':' instead of ';'. A lesser benefit is it allows reuse of the 'for' 
keyword, making the 'foreach' keyword unnecessary.

Maybe this would be acceptable for D?
Nov 01 2010
next sibling parent reply Gary Whatmore <no spam.sp> writes:
Nick Treleaven Wrote:

 There's a C++0x proposal for a range-based 'for' statement:
 http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html
 
 The upcoming GCC 4.6 C++ compiler changes list support for this:
 http://gcc.gnu.org/gcc-4.6/changes.html
 
 I think the syntax could be useful for D to shorten and improve on the 
 status quo a little. Here's the C++ example:
 
 int array[5] = { 1, 2, 3, 4, 5 };
 for (int& x : array)
   x *= 2;
 
 Currently D has:
 
 int array[5] = [1, 2, 3, 4, 5];
 foreach (ref x; array)
   x *= 2;
 
 I think this is better:
 
 for (ref x : array)
   x *= 2;
 
 Apart from being 4 chars shorter, I think it looks more natural using the 
 ':' instead of ';'. A lesser benefit is it allows reuse of the 'for' 
 keyword, making the 'foreach' keyword unnecessary.
 
 Maybe this would be acceptable for D?

No, 1) it's too late to change it. 2) the syntax comes from Java. It would be embarrasing to admit that Java did something right. - G.W.
Nov 01 2010
next sibling parent Pillsy <pillsbury gmail.com> writes:
Gary Whatmore Wrote:
[...]
 It would be embarrasing to admit that Java did something right.

Like class types being reference types, having "abstract" and "final" keywords, only alloowing single inheritance of implementation while allowing multiple implementation of interface, adding garbage collection, making strings immutable...? D is as much Java++ as it is C+=2. Cheers, Pillsy
Nov 01 2010
prev sibling next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 11/1/10 9:09 AM, Gary Whatmore wrote:
 Nick Treleaven Wrote:

 There's a C++0x proposal for a range-based 'for' statement:
 http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html

 The upcoming GCC 4.6 C++ compiler changes list support for this:
 http://gcc.gnu.org/gcc-4.6/changes.html

 I think the syntax could be useful for D to shorten and improve on the
 status quo a little. Here's the C++ example:

 int array[5] = { 1, 2, 3, 4, 5 };
 for (int&  x : array)
    x *= 2;

 Currently D has:

 int array[5] = [1, 2, 3, 4, 5];
 foreach (ref x; array)
    x *= 2;

 I think this is better:

 for (ref x : array)
    x *= 2;

 Apart from being 4 chars shorter, I think it looks more natural using the
 ':' instead of ';'. A lesser benefit is it allows reuse of the 'for'
 keyword, making the 'foreach' keyword unnecessary.

 Maybe this would be acceptable for D?

No, 1) it's too late to change it. 2) the syntax comes from Java. It would be embarrasing to admit that Java did something right. - G.W.

Java did a lot of things right (be they novel or not) that are present in D, such as reference semantics for classes, inner classes with outer object access etc. Andrei
Nov 01 2010
next sibling parent Kagamin <spam here.lot> writes:
Andrei Alexandrescu Wrote:

 Java did a lot of things right (be they novel or not) that are present 
 in D, such as reference semantics for classes, inner classes with outer 
 object access etc.

Implicitly making inner class inner is not right.
Nov 01 2010
prev sibling next sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
On 01/11/2010 15:14, Andrei Alexandrescu wrote:
 On 11/1/10 9:09 AM, Gary Whatmore wrote:
 2) the syntax comes from Java. It
 would be embarrasing to admit that Java did something right.

 - G.W.


Only if one is an idiot.
 Java did a lot of things right (be they novel or not) that are present
 in D, such as reference semantics for classes, inner classes with outer
 object access etc.

 Andrei

A few more things: Annotations: a very flexible and extensible system to add metadata to any kind of definition. Meta-data can be runtime, compile-time or both. D could take a lot of inspiration from Java's annotations. Integrated support for multi-threading: threads, monitors, mutexes/locks, synchronization, etc., are part of the language, including more advanced synchronization constructs such as condition variables. And also a well defined memory model! In fact D took direct inspiration from Java on this, did it not? Also, very good, very well thought concurrency utils (the stuff done by Doug Lea). Wildcards in generics: a very interesting mechanism for increasing type safety. Java wildcards were not done right in every aspect, but still they are very nice, and I don't know of any mainstream languages that have anything quite like that, or even close. -- Bruno Medeiros - Software Engineer
Nov 24 2010
prev sibling parent Kagamin <spam here.lot> writes:
Emil Madsen Wrote:

 Why is reference semantics for classes the right thing to do? - Just curious
 about it, because to me it seems counter intuitive to have to semantics.

What is the alternative? Value semantics?
Nov 25 2010
prev sibling next sibling parent Torarin <torarind gmail.com> writes:
Pilsy:
 D is as much Java++ as it is C+=2.

Andrei:
 Java did a lot of things right (be they novel or not) that are present in D

Even if Java did only a single thing right, it would be silly to not adopt it just to avoid "embarrassment". Anyway, I think it's a nice syntax.
Nov 01 2010
prev sibling next sibling parent Nick Treleaven <nospam example.net> writes:
On Mon, 01 Nov 2010 10:09:17 -0400, Gary Whatmore wrote:

 I think this is better:
 
 for (ref x : array)
   x *= 2;
 
 Apart from being 4 chars shorter, I think it looks more natural using
 the ':' instead of ';'. A lesser benefit is it allows reuse of the
 'for' keyword, making the 'foreach' keyword unnecessary.
 
 Maybe this would be acceptable for D?

No, 1) it's too late to change it.

I don't see a technical reason why foreach, the above for syntax and 'C' for syntax can't all be supported.
 2) the syntax comes from Java.

Perhaps if C++0x does add this and Java already has it this makes more of an argument for D supporting it?
Nov 01 2010
prev sibling next sibling parent so <so so.do> writes:
 Annotations: a very flexible and extensible system to add metadata to  
 any kind of definition. Meta-data can be runtime, compile-time or both.  
 D could take a lot of inspiration from Java's annotations.

 Integrated support for multi-threading: threads, monitors,  
 mutexes/locks, synchronization, etc., are part of the language,  
 including more advanced synchronization constructs such as condition  
 variables. And also a well defined memory model! In fact D took direct  
 inspiration from Java on this, did it not?
 Also, very good, very well thought concurrency utils (the stuff done by  
 Doug Lea).

 Wildcards in generics: a very interesting mechanism for increasing type  
 safety. Java wildcards were not done right in every aspect, but still  
 they are very nice, and I don't know of any mainstream languages that  
 have anything quite like that, or even close.

I don't think that is the case, most of those features are "must" for a modern language, especially a language like D. You don't need a language inspiration for these features you listed at this age PL. -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Nov 24 2010
prev sibling parent Emil Madsen <sovende gmail.com> writes:
--90e6ba539efa430b5a0495d46f09
Content-Type: text/plain; charset=ISO-8859-1

On 1 November 2010 16:14, Andrei Alexandrescu <SeeWebsiteForEmail erdani.org
 wrote:

 On 11/1/10 9:09 AM, Gary Whatmore wrote:

 Nick Treleaven Wrote:

  There's a C++0x proposal for a range-based 'for' statement:
 http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html

 The upcoming GCC 4.6 C++ compiler changes list support for this:
 http://gcc.gnu.org/gcc-4.6/changes.html

 I think the syntax could be useful for D to shorten and improve on the
 status quo a little. Here's the C++ example:

 int array[5] = { 1, 2, 3, 4, 5 };
 for (int&  x : array)
   x *= 2;

 Currently D has:

 int array[5] = [1, 2, 3, 4, 5];
 foreach (ref x; array)
   x *= 2;

 I think this is better:

 for (ref x : array)
   x *= 2;

 Apart from being 4 chars shorter, I think it looks more natural using the
 ':' instead of ';'. A lesser benefit is it allows reuse of the 'for'
 keyword, making the 'foreach' keyword unnecessary.

 Maybe this would be acceptable for D?

No, 1) it's too late to change it. 2) the syntax comes from Java. It would be embarrasing to admit that Java did something right. - G.W.

Java did a lot of things right (be they novel or not) that are present in D, such as reference semantics for classes, inner classes with outer object access etc. Andrei

Why is reference semantics for classes the right thing to do? - Just curious about it, because to me it seems counter intuitive to have to semantics. -- // Yours sincerely // Emil 'Skeen' Madsen --90e6ba539efa430b5a0495d46f09 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On 1 November 2010 16:14, Andrei Alexandrescu <s= pan dir=3D"ltr">&lt;<a href=3D"mailto:SeeWebsiteForEmail erdani.org">SeeWeb= siteForEmail erdani.org</a>&gt;</span> wrote:<br><blockquote class=3D"gmail= _quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:= 1ex;"> <div><div></div><div class=3D"h5">On 11/1/10 9:09 AM, Gary Whatmore wrote:<= br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Nick Treleaven Wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> There&#39;s a C++0x proposal for a range-based &#39;for&#39; statement:<br> <a href=3D"http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.ht= ml" target=3D"_blank">http://www.open-std.org/JTC1/SC22/WG21/docs/papers/20= 09/n2930.html</a><br> <br> The upcoming GCC 4.6 C++ compiler changes list support for this:<br> <a href=3D"http://gcc.gnu.org/gcc-4.6/changes.html" target=3D"_blank">http:= //gcc.gnu.org/gcc-4.6/changes.html</a><br> <br> I think the syntax could be useful for D to shorten and improve on the<br> status quo a little. Here&#39;s the C++ example:<br> <br> int array[5] =3D { 1, 2, 3, 4, 5 };<br> for (int&amp; =A0x : array)<br> =A0 x *=3D 2;<br> <br> Currently D has:<br> <br> int array[5] =3D [1, 2, 3, 4, 5];<br> foreach (ref x; array)<br> =A0 x *=3D 2;<br> <br> I think this is better:<br> <br> for (ref x : array)<br> =A0 x *=3D 2;<br> <br> Apart from being 4 chars shorter, I think it looks more natural using the<b= r> &#39;:&#39; instead of &#39;;&#39;. A lesser benefit is it allows reuse of = the &#39;for&#39;<br> keyword, making the &#39;foreach&#39; keyword unnecessary.<br> <br> Maybe this would be acceptable for D?<br> </blockquote> <br> No, 1) it&#39;s too late to change it. 2) the syntax comes from Java. It wo= uld be embarrasing to admit that Java did something right.<br> <br> =A0- G.W.<br> </blockquote> <br></div></div> Java did a lot of things right (be they novel or not) that are present in D= , such as reference semantics for classes, inner classes with outer object = access etc.<br><font color=3D"#888888"> <br> Andrei<br> </font></blockquote></div><br>Why is=A0reference semantics for classes the = right thing to do? - Just curious about it, because to me it seems counter = intuitive to have to semantics.<br clear=3D"all"><br>-- <br>// Yours sincer= ely<br> // Emil &#39;Skeen&#39; Madsen<br> --90e6ba539efa430b5a0495d46f09--
Nov 24 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Monday, November 01, 2010 06:16:47 Nick Treleaven wrote:
 There's a C++0x proposal for a range-based 'for' statement:
 http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2009/n2930.html
 
 The upcoming GCC 4.6 C++ compiler changes list support for this:
 http://gcc.gnu.org/gcc-4.6/changes.html
 
 I think the syntax could be useful for D to shorten and improve on the
 status quo a little. Here's the C++ example:
 
 int array[5] = { 1, 2, 3, 4, 5 };
 for (int& x : array)
   x *= 2;
 
 Currently D has:
 
 int array[5] = [1, 2, 3, 4, 5];
 foreach (ref x; array)
   x *= 2;
 
 I think this is better:
 
 for (ref x : array)
   x *= 2;
 
 Apart from being 4 chars shorter, I think it looks more natural using the
 ':' instead of ';'. A lesser benefit is it allows reuse of the 'for'
 keyword, making the 'foreach' keyword unnecessary.
 
 Maybe this would be acceptable for D?

The current syntax has been around for quite a while, it's in TDPL, and making the change gives no significant improvement. Generally speaking, we're not really supposed to be making any major changes from what TDPL says without a really good reason. Saving a few characters is not a really good reason. foreach works just fine and is arguably clearer, since you know right away that you're dealing with a foreach loop rather than a for loop - and while similar, they _are_ semantically different. And I don't see why : would be better than ;. IIRC, Java uses one and C# uses the other and you have to remember which uses which when moving between the two. And if you're looking to save typing, ; has the advantage that you don't have to use the shift key. If D had chosen to just reuse for, that would have been fine, but it didn't, and I think that it's fine as it is. Changing foreach to for would not be a productive change in the least. - Jonathan M Davis
Nov 01 2010
prev sibling parent Nick Treleaven <nospam example.net> writes:
On Mon, 01 Nov 2010 09:07:29 -0700, Jonathan M Davis wrote:

 for (ref x : array)
   x *= 2;
 
 Apart from being 4 chars shorter, I think it looks more natural using
 the ':' instead of ';'. A lesser benefit is it allows reuse of the
 'for' keyword, making the 'foreach' keyword unnecessary.
 
 Maybe this would be acceptable for D?

The current syntax has been around for quite a while, it's in TDPL, and making the change gives no significant improvement. Generally speaking, we're not really supposed to be making any major changes from what TDPL says without a really good reason. Saving a few characters is not a really good reason. foreach works just fine and is arguably clearer, since you know right away that you're dealing with a foreach loop rather than a for loop - and while similar, they _are_ semantically different.

OK.
 And I don't see why : would be better than ;. IIRC, Java uses one and C#
 uses the other and you have to remember which uses which when moving
 between the two.

Actually C# uses: foreach (int x in array) So D has its own unique syntax.
 And if you're looking to save typing, ; has the
 advantage that you don't have to use the shift key.

True, but still takes longer to type 'each' than press shift IMO. But readibility is perhaps more important than writing code.
 If D had chosen to just reuse for, that would have been fine, but it
 didn't, and I think that it's fine as it is. Changing foreach to for
 would not be a productive change in the least.

Fair enough.
Nov 01 2010