euddraft
euddraft copied to clipboard
eps: Tuple/List indeces must be integers or slices, not EUDVariable
Sorry to disturb again with this question.
In my eps file, I've written a funtion which returns a list (or a tuple?) of numbers, like
function shuffle(.....) {
const ret = EUDArray(8);
....
return ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], ret[8];
}
function afterTriggerExec() {
const x = shuffle(.....);
for (var i = 0; i < 8; i++) {
.... = x[i];
....
}
}
Then I got the error message "Tuple/List indeces must be integers or slices, not EUDVariable".
The only way I could come up with is:
const x_array = [x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]];
But this makes my code looks bad.
Is there a clean way to deal with this kind of problem? Or could you improve the compiler so that a list is subscriptable with EUDVariable?
Just return EUDArray
, you don't need to return multiple values in this case.
function shuffle(.....): EUDArray { // Clarify return type
const ret = EUDArray(8);
...
// return ret[0], ret[1], ret[2], ret[3], ret[4], ret[5], ret[6], ret[7], ret[8];
// by the way, this code is wrong since EUDArray(8)'s index range is 0~7.
return ret;
}
function afterTriggerExec() {
const x = shuffle(...);
for (var i = 0; i < 8; i++) {
... = x[i]; // type of i is EUDVariable
...
}
}
Thx a lot this really helps!
What about if I wanna define an EUDArray using a function in an if
statement or nested if
statement
const x = EUDArray(8);
if (case1) {x = shuffle(1);}
else {x = shuffle(2);}
for (var i = 0; i < 8; i++) {...x[i]...}
...
Of course the above code won't compile, since x
is const and cannot be reassigned.
If I write:
if (case1) {x = const shuffle(1);}
else {x = const shuffle(2);}
for (var i = 0; i < 8; i++) {...x[i]...}
then there would be an error when compiling: "Undefined rvalue x, assuming as variable". Insterestingly, if I omit the curly brackets in the else statement:
if (case1) {x = const shuffle(1);}
else x = const shuffle(2);
for (var i = 0; i < 8; i++) {...x[i]...}
the code could be compiled, but the value of x
would be assigned to shuffle(2) no matter case1
is true or false.
Is this a compiler problem? Or how should I define an EUDArray using function in if statement?
function shuffle(x): EUDArray {
const ret = EUDArray(8);
for(var i = 0; i <= 7; i++) {
ret[i] = x;
x *= 2;
x += 1;
}
return ret;
}
function afterTriggerExec() {
var x;
if(x) {
x = shuffle(1);
} else {
x = shuffle(2);
}
const a = EUDArray.cast(x);
for(var i = 0; i <= 7; i++) {
simpleprint(a[i]);
}
}
I should note that function shuffle
always return same value (same array ret
).
If you want shuffle
to return unique instance on every function call, you should define object
and use .alloc()
for dynamic instantiation, since EUDArray
doesn't have .alloc()
method.
Related link: https://github.com/phu54321/euddraft/wiki/9B.-Appendix---Static-or-Dynamic-instantiation
Got it. Thank you very much. So eps behaves just C++