I want to call 2 different iterators conditionally:
import os
type FsOrTest* = object
case kind: bool
of true: dir: string
of false: testFiles: seq[string]
iterator walk*(dir: FsOrTest): string =
case dir.kind
of true: dir.dir.walkDirRec()
of false: dir.testSources.items()
But it seems like the compiler doesn't like the types:
/home/xxx/dev/playground/nim/weblang/src/example.nim(10, 19) Error: attempting to call routine: 'walkDirRec'
found os.walkDirRec(dir: string, yieldFilter: set[PathComponent], followFilter: set[PathComponent], relative: bool, checkDir: bool) [iterator declared in /home/xxx/.choosenim/toolchains/nim-1.6.12/lib/pure/os.nim(2371, 10)]
Is there any way to make this work or do I have to use a macro?
Please read this: https://nim-lang.org/docs/manual.html#iterators-and-the-for-statement
iterators are not used like procedures.
iterator m2(x: int): int =
for i in 1..x:
if i mod 2 == 0:
yield i
iterator m3(x: int): int =
for i in 1..x:
if i mod 3 == 0:
yield i
iterator m(m, x: int): int =
if m == 2:
for i in m2(x):
yield i
else:
for i in m3(x):
yield i
for i in m(2, 12):
echo i