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.5k stars 1.46k forks source link

Openarray and iterable typeclasses do not match properly. #18697

Open beef331 opened 3 years ago

beef331 commented 3 years ago

Iterable cannot be combined with other types for a typeclass.

Example

type IntIter = openArray[int] or iterable[int]

template doThing(a: IntIter) =
  for x in a:
    echo x

iterator tIter(a: int): int =
  for x in 0..a:
    yield x

doThing([10, 20, 30, 40])
doThing(tIter(100))

Current Output

Error: type mismatch: got <array[0..3, int]>
but expected one of:
template doThing(a: IntIter)
  first type mismatch at position: 1
  required type for a: IntIter
  but expression '[10, 20, 30, 40]' is of type: array[0..3, int]

expression: doThing([10, 20, 30, 40])

Expected Output

Compiles fine.

Possible Solution

If the only matches are ones that use iterable[T], check if items/pairs match the T.

beef331 commented 3 years ago

After further investigation, seems it's the same for both openArray and iterable using either in a type class results in failure. So relates to: https://github.com/nim-lang/Nim/issues/15722

proc doThing(a: openArray[int] or int) = echo a
doThing(@[10, 20, 30]) # This fails
doThing([10, 20, 30]) # This aswell
#doThing(1) This works
timotheecour commented 3 years ago

see https://github.com/nim-lang/RFCs/issues/397