Skip to content

[RFC] Better iterators syntax #1

@narimiran

Description

@narimiran

Problem

Iterator syntax as currently implemented leaves a lot to be desired.

As seen in this forum post, there are couple of problems with current syntax:

  • instantiating iterators with let x = fibo (without () and without parameters)
  • calling the existing iterator with parameters which might ((1, 1) at the first call) or might not ((10, 20), (100, 999)) have anything to do with the current iterator
  • wrapping iterator inside of a proc helps a bit regarding a better-looking syntax when calling the existing iterator

Proposal:

Use next to call the existing iterator. (Similar to other languages, e.g. Python)

iterator fibo(a, b: int): int = #1: some pragma here?
  var
    a = a
    b = b
  while true:
    yield a
    (a, b) = (b, a+b)

let  #2: instatiating iterators with their starting parameters
  x = fibo(1, 1)
  y = fibo(34, 55)

for _ in 1 .. 5:
  echo next(x)  #3: calling the existing iterator `x` - cannot pass any new parameter once the iterator is instatiated

echo next(y)  #4: first call of `y`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions