Signed-unsigned comparisons in Phobos

Marco Leise Marco.Leise at gmx.de
Fri Aug 12 06:53:44 PDT 2011


Am 12.08.2011, 15:44 Uhr, schrieb Timon Gehr <timon.gehr at gmx.ch>:

> On 08/12/2011 03:33 PM, Marco Leise wrote:
>> Am 12.08.2011, 15:08 Uhr, schrieb bearophile <bearophileHUGS at lycos.com>:
>>
>>> Marco Leise:
>>>
>>>> Just remember that reverse loops are written like this:
>>>>
>>>> for (size_t i = x.length; i-- > 0; ) {...}
>>>
>>> Thankfully in D there is foreach_reverse :-)
>>>
>>> import std.stdio;
>>>
>>> void main() {
>>> auto array = [10, 20, 30];
>>>
>>> for (size_t i = array.length; i-- > 0; )
>>> writeln(i, " ", array[i]);
>>>
>>> foreach_reverse (i, item; array)
>>> writeln(i, " ", item);
>>> }
>>>
>>> Bye,
>>> bearophile
>>
>> Sure, but we were talking about for, not foreach. Maybe these cases in
>> Phobos cannot use foreach, because they modify 'i' or they don't require
>> 'item'.
>
> foreach_reverse(i;0..array.length) writeln(array[i]),i--;

:D it doesn't crash! Thx, I'll ditch the C syntax from now on!


More information about the Digitalmars-d mailing list