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 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 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 reply 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
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 reply Emil Madsen <sovende gmail.com> writes:
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
Nov 24 2010
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 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 parent reply 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 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
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.

 uses the other and you have to remember which uses which when moving
 between the two.
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