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).
iterator iota(n: int): int =
for i in 0..<n: yield i
template toSeq2[T](a: iterable[T]): seq[T] =
var ret: seq[T]
assert a.typeof is T
for ai in a: ret.add ai
ret
assert iota(3).toSeq2 == @[0, 1, 2]
assert toSeq2(5..7) == @[5, 6, 7]
assert not compiles(toSeq2(@[1,2])) # seq[int] is not an iterable
assert toSeq2(items(@[1,2])) == @[1, 2] # but items(@[1,2]) is
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .