type
Direct = enum
n, e, s, w
Cell = ref CellObj
CellObj = object
row : 0..32
col : 0..32
links : array[Direct, Cell]
Grid {.inheritable.} = object
rows : 0..32
cols : 0..32
grid : array[0..32, array[0..32, Cell]]
#Note that size of array must be allocated at compile time
# but it can be allocated over a range
proc newCell(row, col: int8): Cell =
result.row = row
result.col = col
proc newGrid(rows, cols: int8): Grid =
result.rows = rows
result.cols = cols
for i in 0..rows:
for j in 0..cols:
result.grid[i, j] = newCell(i, j)
grid.nim(27, 20) Error: type mismatch: got (array[0..32, array[0..32, Cell]], int8, int8, Cell) but expected one of: system.[]=(s: var seq[T], x: Slice[int], b: openarray[T]) system.[]=(s: var string, x: Slice[int], b: string) system.[]=(a: var array[Idx, T], x: Slice[int], b: openarray[T]) system.[]=(a: var array[Idx, T], x: Slice[Idx], b: openarray[T])
Try with the array indexing like
result.grid[i][j] = newCell(i, j)
because you have a nested type. You can overload [] to get the other way too if you wish.
Thanks. That worked fine.
I do want to index the other way, but I'm going to need to be more comfortable with nim before I try it.