Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Passing empty array to openarray parameter of template fails

Open Perelandric opened this issue 9 years ago • 3 comments

The last line of the following code example produces a compile-time error:

template test(a: openarray[int]) =
  for n in a:
    echo n

test([1,2,3]) # This works
test([])      # This fails

lib/system.nim(1695, 22) Error: invalid type: 'empty' in this context: 'iterator (array[0..-1, empty]): iter[empty]{.inline, gcsafe, locks: 0.}'

No such error when it's a proc, only a template.

Not sure if it's related to the behavior described here https://github.com/Araq/Nim/issues/1105#issuecomment-87214631 , which I tested and found to still exist, but thought I'd mention it anyway.

Nim Compiler Version 0.10.3 (2015-04-03) [Linux: amd64]

Perelandric avatar Apr 04 '15 17:04 Perelandric

Still an issue.

Araq avatar Oct 15 '18 00:10 Araq

I stumbled into this as well in 1.4.8, pretty annoying one.

template test(a: openarray[int]) =
  for n in a:
    echo n

const x = []
test(x)

fails with:

test.nim(6, 5) template/generic instantiation of `test` from here
test.nim(3, 10) Error: type mismatch: got <empty>
but expected one of: 
proc echo(x: varargs[typed, `$`])
  first type mismatch at position: 1
  required type for x: varargs[typed]
  but expression 'n`gensym0' is of type: empty

expression: echo n`gensym0

kunitoki avatar May 29 '21 13:05 kunitoki

Gives Error: cannot infer element type of items([]) on the devel

ringabout avatar Jan 25 '24 14:01 ringabout