Hi. I have a little question: what difference between samples? And why first sample works? I think that there must be each items, not pairs, no?
for index, item in ["a","b"]:
echo item, " at index ", index
# => a at index 0
# => b at index 1
for index, item in ["a","b"].pairs:
echo item, " at index ", index
# => a at index 0
# => b at index 1
why first sample works?
Because it is correct.
pairs should give you a tuple, you can use it this way:
for index, item in ["a","b"]:
echo item, " at index ", index
for t in ["a","b"].pairs:
echo t[1], " at index ", t[0]
for index, t in ["a","b"].pairs:
echo t
echo t[1], " at index ", index, "==", t[0]
$ ./t
a at index 0
b at index 1
a at index 0
b at index 1
a
at index 0==a
b
at index 1==b
The last loop does indeed not work as one may desire, but that loop would make no real sense, as it would give us the same index value in two different ways -- as part of the tuple and as index variable.
The reason the first case is correct is because Nim implicitly calls pairs when the for loop has exactly 2 variables.