I need to define the `pairs` iterator from a template, because the template
brings some global symbols, something like:
type
Melon = object
template pairs*(m: Melon): tuple[key: int, val: int] =
iterator pairs(m: Melon): tuple[key: int, val: int] =
for i in 0 .. 10:
yield (key: i, val: i * i)
let m = Melon()
for k, v in m:
echo "k=", k, "; v=", v
Run
This code does not compile with the cryptic error message `toto.nim(10, 13)
Error: cannot use symbol of kind 'let' as a 'param'`. nim check adds a few more
hints:
Error: expression has no type: iterator pairs(m: Melon): tuple[key: int,
val: int] =
for i`gensym4552018 in 0 .. 10:
yield (key: i`gensym4552018, val: i`gensym4552018 * i`gensym4552018)
toto.nim(5, 12) Hint: 'pairs' is declared but not used [XDeclaredButNotUsed]
Run
Could it be because the template and the iterator have the same name? But I
need the template to be named `pairs` to be [implicitly
called](https://nim-lang.org/docs/manual.html#iterators-and-the-for-statement-implict-itemsslashpairs-invocations)
by the compiler, no?
Changing `m` to be a `var` changes the error message to `toto.nim(10, 13)
Error: cannot use symbol of kind 'var' as a 'param'` but it does not make the
error more understandable.
So is it possible to write an implicite `pairs` iterator from a template?