extend foreach to work on non-arrays

IntegratedDimensions IntegratedDimensions at gmail.com
Thu May 24 23:27:12 UTC 2018


On Thursday, 24 May 2018 at 23:03:34 UTC, meppl wrote:
> On Thursday, 24 May 2018 at 22:43:00 UTC, IntegratedDimensions 
> wrote:
>> Doesn't make any sense?
>>
>> foreach(a; x)
>>
>> if x is not an array then a = x and the loop reduces simply 
>> and function to the case it is not so their can be no harm.
>>
>> It unifies the concepts so that one does not have to worry if 
>> x is an array or not and can offer no harm since when x is not 
>> an array everything simply reduces to an an alias of x.
>
> on the otherhand some programmer might want to get informed buy 
> an error-msg, if he accidentally used a non-iteratable variable 
> for `foreach`-iteration. To avoid a silent bug

In this case it cannot be a bug. The foreach is simply 
ignored/reduced. It would be impossible for any bug to creep 
in(assuming no compiler bugs) in such cases.

foreach(a; x)
{
    x[]
}

would be some type of potential bug... but that bug would also 
exist without the loop. If x is not an array then the the foreach 
effectively is removed and a is just auto a = x;

Any code that uses x would fail just as much as using a and no 
more except.





More information about the Digitalmars-d mailing list