Hi all, I would like to know how can i change the iterating operator in this code. This code snippet is from "Nim by Example"
# Give it a different name to avoid conflict
iterator `...`*[T](a: T, b: T): T =
var res: T = T(a)
while res <= b:
yield res
inc res
for i in 0...5:
echo i
But i want to change the "..." operator to "To". So i wrote this
# Give it a different name to avoid conflict
iterator `To`*[T](a: T, b: T): T =
var res: T = T(a)
while res <= b:
yield res
inc res
for i in 0 To 5:
echo i
But it gave this error message
Error: attempting to call routine: 'To' found 'in.To(a: T, b: T) [declared in /usercode/in.nim(2, 10)]' of kind 'iterator'
I dont know how 0...5 works but 0To5 is not valid syntax.
0.To(5) or 0.To 5 works though.
template to(a, b): untyped = a..b
for i in 0.to(5):
echo i
You're better off using .. though because every Nim programmer knows it and eventually it will be familiar to you too.