-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IndexError when use a list of funcs generated bymapit
#12625
Comments
Regarding the expected output, running the example outside of the static block gives:
|
Here is an example that can't give the correct result running in if isMainModule:
let m = @[func (x: int): int = x + 100, func (x: int): int = x + 200]
let l = m.mapIt(func (x: int): int = it(x))
echo l.mapIt(it 1) Current Output
Expected Output
|
hm, is |
It doesn't matter, I originally reproduced the problem without it. |
My finding: So, the title of this issue should be changed to: if isMainModule:
let m = @[func (x: int): int = x + 100, func (x: int): int = x + 200]
# The "simplified" generated C code of this line is listed below
let l = m.mapIt(func (x: int): int = it(x))
echo l.mapIt(it 1) The "simplified" generated C code: it__MpefkJ9cQAwQD9aL17D5DVvw; // *it* is a global variable, which is wrong!
N_LIB_PRIVATE N_NIMCALL(NI, colonanonymous___vsp4xqNMQqR9b4HAKLhepJw_3)(NI x) { NI result;
result = it__MpefkJ9cQAwQD9aL17D5DVvw(x);
return result;
}
// let l = m.mapIt(func (x: int): int = it(x))
for (int i = 0; i < 2; i++)
{
it__MpefkJ9cQAwQD9aL17D5DVvw = m[i];
l[i].proc = colonanonymous___vsp4xqNMQqR9b4HAKLhepJw_3;
l[i].env = NIL;
} |
This fixes part of the issue nim-lang#12625. vm & other backends also need update.
This fixes part of the issue nim-lang#12625. vm & other backends also need update.
Some updates on this issue. Further checking indicates that the root cause is, in a @Araq pointed out in this thread:
Unfortunately, this workaround does not work for this case. In the |
1. fallback to call `map` when the result of `op` is a closure; 2. use `items(s)` in the for loop.
Use
mapit
to generate a list of functions. When calling these functions, Index error occur.Example
Current Output
Expected Output
Possible Solution
N/A.
Additional Information
N/A
The text was updated successfully, but these errors were encountered: