iterable

A called iterator yielding type T can be passed to a template or macro via a parameter typed as untyped (for unresolved expressions) or the type class iterable or iterable[T] (after type checking and overload resolution).

  1. iterator iota(n: int): int =
  2. for i in 0..<n: yield i
  3. template toSeq2[T](a: iterable[T]): seq[T] =
  4. var ret: seq[T]
  5. assert a.typeof is T
  6. for ai in a: ret.add ai
  7. ret
  8. assert iota(3).toSeq2 == @[0, 1, 2]
  9. assert toSeq2(5..7) == @[5, 6, 7]
  10. assert not compiles(toSeq2(@[1,2])) # seq[int] is not an iterable
  11. assert toSeq2(items(@[1,2])) == @[1, 2] # but items(@[1,2]) is