the link to know what I am talking about
I was studying the tutorial part1 and when I reached this section on Iterators, I found that the example could be better. I would like to suggest 2 improvements; avoid trivial errors and use a different procedure name.
In this section, the tutorial tries to define a countup proc to use in a for loop, just like in the following.
proc countup(a, b: int): int =
var res = a
while res <= b:
return res
inc(res)
The tutorial proceeds to say that this will never work well because this is uses a return(not yield). However, and this is my first point, what the readers will experience is not an error about returning/yielding. The error code tells the readers not to write a statement after a return, which is always important but at the same time, not essential in this context. Thus I would like to suggest using an code that gives a more essential error code, or rather, does not give a trivial error.
My second point focuses on what happens after the readers manage to solve this problem by themselves. Say, they managed to rewrite the code with something like:
proc countup(a, b: int): int =
var res = a
while res <= b:
result = res
inc(res)
and continue the code with the following, expecting an error like "countup is a not an iterator but a procedure so cannot be used in a for loop" or sth else
for i in countup(1,9)
echo i
However, what awaits the reader is an output that seems to have nothing to do with errors. The reason is simple; the Nim compiler is cleaver enough not to pick the user defined countup proc and to choose the built-in count iterator.
I believe that other readers will be as puzzuled as me to see a wrong code working without defect and at the same time, I believe that this complication can be avoided by using a different proc name such as proc mycountup.
What do you think about this?