I know that Nim supports first class iterators, and a few standard iterators over ranges are defined, such as countup. According to the documentation, .. receives no special treatment, but it is just an alias for countup.
What is, then, the meaning of ..<? I would imagine that - like .. - it is just a symbol bound to an iterator in the standard library. What it weird, though, is that all of the following are valid:
for i in 0 ..< 10:
for i in 0 .. < 10:
for i in 0 .. < 10:
Is the parser somewhat aware of this particular iterators?
On a related note, I can also use the same syntax to index into collections, like this
someSeq[12..24]
When I have tried this with custom collections, I have seen that Nim tries to pass a Slice to the [] function. But isn't 12..24 an iterator? My first guess would be that there is a converter between iterators and slices, but then I saw that .. is declared as an inline iterator, and - if I understand correctly - this means it is not a first class entity, hence I am not even sure what such a converter would mean.
In short: to what extent are .. and ..< special syntax for Nim and to what extent are simply a clever use of language features, such as operator overloading, [] overloading and first-class iterators?
While I am currently learning about Nim I was curious about this topic. But as of today I get undeclared identifier for ..<, while .., < and .. < do what I expected after reading this topic.
@andrea: I don't understand what could be the difference between you second and third "for i in..." example. To me it looks the same just once with a single space and the other time with double space between the operators. Is the spacing supposed to making a difference there?
I also found that let x = 1..10 defines a slice, not an iterator. It can be used with v[x] but not in for in x:.
template `..<`(a, b): expr = a .. < b
I actually ran into this particular error ("undeclared identifier ..<", or something like that) when I tried to compile one of the first little nim programs that I wrote while going through the nim tutorials.
This seems to be a pretty common idiom. Wouldn't it make sense to predefine ..< as a template, as suggested in this thread?
Araq, thanks! I'll probably use it.
But there's also this needed:
template `..-`(a, b): expr = a .. -b
for i in -5..-1: echo i