Multidimensional StaticArray

Hi!

When I try to build a multidimensional StaticArray there is a strange behavior. For example:

#root level (NOT working at all)
a = StaticArray[StaticArray[0, 1, 2], StaticArray[3, 4, 5], StaticArray[6, 7, 8]]

…then, I tried to wrap it in a function:

def start

 a = StaticArray[StaticArray[0, 1, 2], StaticArray[3, 4, 5], StaticArray[6, 7, 8]]

 #silently NOT working!
 a[1][1] = 5

end

start

Am I doing something wrong? Or is it a bug?

StaticArrays are value types because they are a struct.

On the first a[1] you are getting a copy of StaticArray[3, 4, 5]. You are succesfully modifying it, but it’s a copy.

Modifying nested structs is sometimes possible, but it depends on some subtleties. Structs with plain properties work fines thanks to some heuristics (or quirks) in the compiler that inline single line methods. This seems to not be the case of the StaticArray#[] method.

(NB: it’s strange that there are @[AlwaysInline], I’m not sure).

As a workaround a.update 1 { |r| r[1] = 5; r }

Or use another representation for the matrix, like a sole StaticArray for the entire matrix :-)

Workaround works fine. Sole StaticArray is also a possibility. Thank you!