Skip to content

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

Open
@timotheecour

Description

@timotheecour

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions