7

From docs:

In Solidity, array dimensions are declared backwards from the way you might be used to declaring them in C or Java, but they are access as in C or Java.

For example, int8[][5] somearray; are 5 dynamic int8 arrays.

The reason for this is that T[5] is always an array of 5 T's, no matter whether T itself is an array or not (this is not the case in C or Java).

The question is why must in Solidity "T[5] is always an array of 5 T's" ?

10

There is no any limitation, this is just syntactic feature. It is quite unusual but has some rationale behind it.

E.g. in C language int8 x[5] reads as "x is an array of 5 int8 elements". And this one int8 y[5][4] is "y is an array of 5 arrays of 4 int8 elements each".

Note that order of terms x, int8, 5, 4 in declaration and in English explanation is not the same.

In Solidity int8[5][4] x reads as "x is array of 4 arrays of 5 int8 elements each". You need to read backwards but order of terms is now consistent.

Go language goes one step further — it has terms in order and does not require you to read backwards: var a [5][4]int is "variable a is array of 5 arrays of 4 int elements each".

So, "T[5] is always an array of 5 T's" means that for any type T (be it int8 or bool[3][2]) T[5] will always be array of 5 elements. This is not true for some languages like C: T[5] could be

  • array of 5 ints: int x[5] (T is int x)
  • array of 4 arrays of ints: int x[4][5] (T is int x[4])
  • ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.