I want to 2D array (float type) for data buffer, but it seems not so much document/examples about how to construct a 2D array. The only one example could be found by me is from this one (https://nim-by-example.github.io/arrays/)
So my code is as below.
const
Imax = 126
Jmax = 126
type
Matrix[W, H: static[int]] =
array[W, array[H, float]]
var v:Matrix[Imax+1, Jmax+1]
I can use v[0][0] = 1.5 such expressions to assign values, and the 2D array works in my code as data buffer.
I think your code is OK when you need a generic 2D array. The static[int] tells the compiler that the value is a compile time value. Of course size of array is a compile time constant. Maybe the compiler is smart enough so you do not need static here.
If I do need only one array I do it this way:
const
Rows = 8
Cols = 8
type
Row = array[Cols, int]
Board = array[Rows, Row]
var b: Board
b[0][7] = 4
echo b[0][7]
Some languages would allow writing b[0, 7] instead of b[0][7].
When performance matters, you may use an array with only one index and do the math to access fields yourself, that may be faster, but when you do it bad it may be slower. (ie add (8+1) to position to move chess piece diagonal.)
Just FYI - you can write b[0, 7] in Nim too with ease
const
Rows = 8
Cols = 8
type
Row = array[Cols, int]
Board = array[Rows, Row]
proc `[]`(b: Board, r, c: int): int =
b[r][c]
var b: Board
b[0][7] = 4
echo b[0][7]
echo b[0, 7]
Wow, I always thought that was a languages limitation or a conflict with other Nim syntax!