Consider this example:
iterator fibo(a, b: int): int {.closure.} =
var
a = a
b = b
while true:
yield a
(a, b) = (b, a+b)
let x = fibo
for _ in 1 .. 5:
echo x(1, 1)
echo x(10, 20)
echo x(100, 999)
echo "\n##########\n"
let
y = fibo
z = y(5, 8)
for i in 1 .. 5:
echo z
In the first case, once you start the iterator, passed values don't matter. Why
then even allow it?
In the second case, I thought about passing the values just the first time when
defining z and then just call it, to reduce the confusion. Can't do.
* * *
For me, the expected syntax/behaviour would be something like this:
let c = fibo(1, 1)
for i in 1 .. 5:
echo next(c)
* * *
What do you guys think? Is this strange/confusing just for me?
Can the above example with next (or something similar to it) somehow be
implemented in current version of Nim? (please don't just answer with "use
macros" I have no experience with them - an example how to do it would be great)