shorter foreach syntax - C++0x range-based for

Emil Madsen sovende at gmail.com
Wed Nov 24 14:58:59 PST 2010


On 1 November 2010 16:14, Andrei Alexandrescu <SeeWebsiteForEmail at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20101124/32e2e317/attachment.html>


More information about the Digitalmars-d mailing list