Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for x in myIter(1)(): ... gives infinite loop; inconsistent with let it = iterGen(1); for x in it(): ... #8775

Open
timotheecour opened this issue Aug 25, 2018 · 1 comment

Comments

@timotheecour
Copy link
Member

timotheecour commented Aug 25, 2018

originally reported here: https://github.com/nim-lang/Nim/issues/7047#issuecomment-390604711

proc iterGen(someEvalOnceArg: int): (iterator (): int {.closure.}) =
  result = iterator (): int =
    yield 1
    yield 2
    yield 3

# this works fine
let it = iterGen(42)
for x in it():
  echo x

# this is an infinite loop
for x in iterGen(42)():
  echo x

proposal

  • make for x in expr: ... cache the value of expr to avoid this (maybe using something like sequtils.evalOnceAs)

scratch below

  • maybe for could be actually implemented in system.nim, now that we have forLoopMacros; maybe this would help in solving this (not sure)
@timotheecour timotheecour changed the title for x in myIter(42)(): ... gives infinite loop; inconsistent with let it = iterGen(42); for x in it(): ... for x in myIter(1)(): ... gives infinite loop; inconsistent with let it = iterGen(1); for x in it(): ... Aug 25, 2018
@bluenote10
Copy link
Contributor

This has also been proposed here #2563

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants