Am 08.01.21 um 17:16 schrieb Allin Cottrell:
On Fri, 8 Jan 2021, Artur Tarassow wrote:
> Am 08.01.21 um 15:12 schrieb Allin Cottrell:
>>
>> "loop foreach" is intended for iterating across strings, and allowing
>> iteration over bundle keys and arrays of strings is a special case of
>> that. But in general "foreach" for arrays is redundant since they are
>> indexed and ordered.
>
> Thanks for the clarification, Allin. I agree that with your redundancy
> argument to some degree.
>
> However, I've (still) have two remarks on the current behavior:
>
> 1) Using a foreach loop looks very natural (at least) to me when
> iterating over bundles and matrices -- even though it may be redundant
> [...]
OK, I take your point. Now in git
loop foreach i <array>
is taken as syntactic sugar for
loop i=1..nelem(<array>)
(If <array> is not an array of strings, $i just gives the string
representation of the index.)
> 2) If the foreach type of loop is not supported for matrices and
> bundles, I think it should not work at all when iterating over these
> data types and actually throw an error.
This goes back to the original point of a foreach loop, namely iterating
over strings (with string substitution supported via $i). From that
point of view, when you said
loop foreach i B
...
endloop
it would mean: iterate over an array of literal strings containing the
single element "B". The fact that "B" might be the name of an array
was
irrelevant, and ignored. But since that interpretation has already been
breached for bundles and arrays of strings, it might as well be breached
for all array types.
(Note: you still get the traditional interpretation if "foreach i" is
followed by more than one string.)
Thanks for this, Allin. I've also added tests for this to our test suite.
Artur