foreach/iota countdown

Sergei Nosov sergei.nosov at gmail.com
Tue Feb 18 00:45:37 PST 2014


On Tuesday, 18 February 2014 at 05:21:24 UTC, Brian Schott wrote:
> On Monday, 17 February 2014 at 19:22:38 UTC, simendsjo wrote:
>> Should the following two uses be a compile-time error?
>>  foreach(i; 10 .. 0) // Never executes
>>
>>  foreach(i; iota(10, 0)) // .. neither does this
>>
>> I would like the second to either be a compile-time error or 
>> automagically use a negative step.
>>
>> So we need to use a negative step in iota() or use a for loop
>>  foreach(i; iota(10, 0, -1)) // as expected
>
> I just added this check to DScanner:
>
> -------------
> import std.stdio;
>
> void main(string[] args)
> {
> 	auto x = args[3 .. 2];
> 	foreach (i; 20 .. 10)
> 	{
> 	}
> }
> -------------
> /home/alaran/tmp/test.d(5:16)[warn]: 3 is larger than 2. This 
> slice is likely incorrect.
> /home/alaran/tmp/test.d(6:22)[warn]: 20 is larger than 10. Did 
> you mean to use 'foreach_reverse( ... ; 10 .. 20)'?

Isn't foreach_reverse being deprecated?


More information about the Digitalmars-d-learn mailing list