Hi,
I'm going through the "learn nim by example" tutorial and there is something I don't understand on the "loops and iterators" section (http://nim-by-example.github.io/for_iterators/).
What is the difference between a regular iterator and an inline iterator? It is not clear to me from that page.
iterator countTo(n: int): int {.inline.} =
var i = 0
while i <= n:
yield i
inc i
for i in countTo(5):
echo i
is transformed in-place to something like
let n = 5
var i = 0
while i <= n:
echo i
inc i
Every for i in countTo(5): echo i is transformed to that code.
For closure iterators the code of iterator is inserted only once in your .exe, and countTo(5) is like a function call, so has its overhead.
And closure iterators are first-class, you can pass them as arguments to procs or assign to variables.
I see. That makes sense! Then it seems that the example is wrong. It is missing the {.inline.} directive:
iterator countTo(n: int): int =
var i = 0
while i <= n:
yield i
inc i
for i in countTo(5):
echo i
In that case the iterator will be implicitly inline.
Why? that is the part that I don't understand.