digitalmars.D - shorter foreach syntax - C++0x range-based for
- Nick Treleaven (20/20) Nov 01 2010 There's a C++0x proposal for a range-based 'for' statement:
- Gary Whatmore (3/32) Nov 01 2010 No, 1) it's too late to change it. 2) the syntax comes from Java. It wou...
- Pillsy (6/7) Nov 01 2010 Like class types being reference types, having "abstract" and "final" ke...
- Andrei Alexandrescu (5/37) Nov 01 2010 Java did a lot of things right (be they novel or not) that are present
- Torarin (5/7) Nov 01 2010 Even if Java did only a single thing right, it would be silly to not
- Kagamin (2/5) Nov 01 2010 Implicitly making inner class inner is not right.
- Bruno Medeiros (19/28) Nov 24 2010 A few more things:
- so (6/20) Nov 24 2010 I don't think that is the case, most of those features are "must" for a ...
- Emil Madsen (6/50) Nov 24 2010 Why is reference semantics for classes the right thing to do? - Just cur...
- Kagamin (2/4) Nov 25 2010 What is the alternative? Value semantics?
- Nick Treleaven (5/18) Nov 01 2010 I don't see a technical reason why foreach, the above for syntax and 'C'...
- Jonathan M Davis (15/44) Nov 01 2010 The current syntax has been around for quite a while, it's in TDPL, and ...
- Nick Treleaven (8/32) Nov 01 2010 Actually C# uses:
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
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
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
On 11/1/10 9:09 AM, Gary Whatmore wrote:Nick Treleaven 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. AndreiThere'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
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 DEven 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
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
On 01/11/2010 15:14, Andrei Alexandrescu wrote:On 11/1/10 9:09 AM, Gary Whatmore wrote:Only if one is an idiot.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. AndreiA 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
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
On 1 November 2010 16:14, Andrei Alexandrescu <SeeWebsiteForEmail erdani.orgwrote:On 11/1/10 9:09 AM, Gary Whatmore 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. -- // Yours sincerely // Emil 'Skeen' MadsenNick Treleaven Wrote: There's a C++0x proposal for a range-based 'for' statement: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. Andreihttp://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 24 2010
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
On Mon, 01 Nov 2010 10:09:17 -0400, Gary Whatmore wrote:I don't see a technical reason why foreach, the above for syntax and 'C' for syntax can't all be supported.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.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
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
On Mon, 01 Nov 2010 09:07:29 -0700, Jonathan M Davis wrote:OK.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.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