Matthias Walter , dans le message (digitalmars.D:172673), a écrit : > I looked at Bug #6153 (Array!(Array!int) failure) and found that the > > This exactly is what makes the following code fail: > > Array!(Array!int) array2d; > array2d.length = 1; > array2d[0].insert(1); > > The inner array "array2d[0]" was not initialized and hence the reference > pointer is null. Since Array.opIndex returns by value, the 'insert' > method is called on a temporary object and does not affect the inner > array (still being empty) which is stored in the outer array. > > What do you think about this? > > Must the user ensure that the Array container is always initialized > explicitly? If yes, how shall this happen since the only constructor > takes a (non-empty) tuple of new elements. Or shall opIndex return by > reference?
I think opIndex should return by reference. opIndexAssign is of no help when the user want to use a function that takes a reference (here Array.insert). It is normal that Array uses default construction when someone increases the array's length. Besides that point, I don't see why default-constructed Array have an uninitialised Payload. This makes uninitialised Array behaves unexpectedly, because making a copy and using the copy will not affect the original, which is not the intended reference value behavior. Correcting default-initialization of Array would correct your bug, but would not correct the wrong behavior of Array when the value returned by opIndex is used with a non-const method with other objects (dynamic arrays?). So both have to be changed in my opinion. -- Christophe
