Consider this code sample:
proc foo(arr: openArray[string]): proc =
result = proc =
for i in arr:
echo i
const arr = @["qwe",]
discard foo(arr)
This won't compile:
Error: internal error: genTypeInfo(tyOpenArray)
No stack traceback available
To create a stacktrace, rerun compilation with './koch temp js <file>', see https://nim-lang.github.io/Nim/intern.html#debugging-the-compiler for details
However, if I replace openArray with seq, it compiles:
proc foo(arr: seq[string]): proc =
result = proc =
for i in arr:
echo i
const arr = @["qwe",]
discard foo(arr)
Also, iterating over an openArray is fine if it's not done in the returned proc:
proc foo(arr: seq[string]): proc =
for i in arr:
echo i
result = proc =
discard
const arr = @["qwe",]
discard foo(arr)
I think this may be a bug in the compiler that needs to be reported but I wanted to make sure it's not me doing some invalid stuff before I report it.
I do not understand the code, you try to do a Closure?, I dont think a bare proc like that is a concrete type.
It smells like a bug anyway by that compiler message, maybe can be reported.
I do not understand the code, you try to do a Closure?
This is just a stripped down code sample to demo the issue.
I'm working on a router for Karax. To do that, I need a proc that returns a proc. And this proc in turn would have to check the available routes, meaning iterating over them.
It's not that exotic really :-)
I dont think a bare proc like that is a concrete type.
It is, the syntax is fine. It's a proc without args that returns void.
Again, it's just a sample; in real code, it's more interesting.
It smells like a bug anyway by that compiler message, maybe can be reported.
I'm now confident it is, because simply adding tyOpenArray to https://github.com/nim-lang/Nim/blob/devel/compiler/jstypes.nim#L136 did the trick.
I'll report it and submit a PR.
If I'm not mistaken, you can't really close over an openArray because it's just a pointer+size pair
I think, from the JS backend perspective, open array and sequence are indistinguishable.
It's just that currently openArray is just missing from compiler/jstypes.nim entirely, so I think it may have been simply forgotten.
Error: 'arr' is of type <openArray[string]> which cannot be captured as it would violate memory safety ... using '-d:nimNoLentIterators' helps in some cases
instead of an internal compiler error. However allowing it in the JS backend may cause it to be inconsistent with other backends.
Anyway I think you can change it to:
proc foo(arr: openArray[string]): proc =
var s = @arr
result = proc =
for i in s:
echo i
const arr = @["qwe",]
discard foo(arr)
Thanks for the good point! I'll update my code, I like your variant. It's clear and safe.
My PR that enables this syntax for JS brackend has already been merged. So I think it may stay there. The current behavior is probably better than the obscure compiler error.