Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use variable index in a constant list

Tags:

dml-lang

Using the Device Modeling Language (DML), version 1.4.

​I create a param of lists like

param X = [ [0, 0, 0], [0, 0, 1].......]; 

And I want to access them in a method using variable like

method get_var(uint32 idx) -> (uint32) {
  return X[idx][0];
}

But when I try to compile it says I cannot use variable index in a constant list.

Is there a different data type that I should be using other than param to allow this? Or is there a way to define "idx" as a const so it could be used?

The workaround that I currently have is creating a session variable and copying the param list over during post_init() since then I'm allowed to access that session variable using variable index. I don't know if it's the best way to handle that.

like image 558
Yun Avatar asked May 26 '26 02:05

Yun


1 Answers

Another possibility is to use a session variable. Session variables will take some extra space, and will consume stack space equal to their size during object initialization, so they are not suited for very large arrays. But for reasonable array sizes < 256 it is a valid approach.

session int my_array[3][3] = {{0, 1, 2}, {4, 1, 2}, {1, 2, 3}};

method get_int(int idx) -> (int) {
    return my_array[idx][0];
}
like image 189
Gustav Wiklander Avatar answered Jun 04 '26 17:06

Gustav Wiklander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!