-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
EDIT by @narimiran - simplified example at the top, so our tools can pick it up:
iterator FaiReader(): string {.closure.} =
yield "something"
template toClosure(i): auto =
iterator j: string {.closure.} =
for x in FaiReader():
yield x
j
proc main =
var reader = toClosure(FaiReader())
main()
Somewhat related: #5859
I am trying to pass an iterator to another proc. To do that, I've tried using the toClosure()
template from @def- : https://forum.nim-lang.org/t/2027
Example
https://github.com/bio-nim/pb/blob/internal-error/src/falconcpkg/rotate.nim
(Hopefully that file will compile for you if you copy/paste, as I've commented out the dependencies.)
Current Output
rotate.nim(182, 9) Error: internal error: expr: var not init FaiReader_932476
No stack traceback available
180 template toClosure(i): auto =
181 iterator j: type(i) {.closure.} =
182 for x in i:
183 yield x
184 j
...
199 var reader = toClosure(FaiReader(fin, full_sequence))
Expected Output
I am willing to do this a different way. I just can't figure out how to pass an "iterator" to another proc, the way I would pass a "generator" in Python.
I thought toClosure()
would at least allow me to see the type it returns, using typetraits, but I cannot get this to compile at all.
Possible Solution
No idea. For now, I will simply avoid passing an iterator. But it's a useful pattern if it can work. Again, here is info on passing iterators: https://forum.nim-lang.org/t/2027
Additional Information
$ nim -v
Nim Compiler Version 1.0.99 [Linux: amd64]
Compiled at 2019-09-28
also fails for
Nim Compiler Version 0.20.2 [Linux: amd64]
Compiled at 2019-07-17
I'm on Linux.