nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.45k stars 1.47k forks source link

Passing a generic iterator to another iterator doesn't seem to work #1550

Closed jovial closed 3 years ago

jovial commented 9 years ago
type
  A[T] = iterator(x: T): T {.gcsafe, closure.}
  B = iterator(y: A[int], z: int): int {.gcsafe, closure.}

iterator aimp[T](x: T): T {.gcsafe, closure.} =
  var total = 0
  while (total < 100):
    yield total
    total += x

iterator bimp(y: A[int], z:int): int {.gcsafe, closure.} =
  #var x = y(z)
  for i in y(z):
    yield i

for x in aimp[int](3):
  echo x

var y = aimp[int]
var z = bimp
for x in z(y):
  echo x

This gives:

/tmp/aporia/a16.nim(23, 12) Error: type mismatch: got (int)
but expected one of:
system.items(a: cstring): iter[char]
...

Uncommenting #var x = y(z), yields:

/tmp/aporia/a16.nim(31, 10) Error: type mismatch: got (iterator (int): int{.closure, gcsafe.})
but expected one of:
iterator (A[int], int): int{.closure, gcsafe.}

which seems incorrect.

ringabout commented 3 years ago

Original codes are not correct:

var y = aimp[int]
var z = bimp
for x in z(y):
  echo x

It should give another parameters(int type) to z. For example:

var y = aimp[int]
var z = bimp
for x in z(y, 1):
  echo x

which works since Nim v1.2.6