On 11/19/2016 10:26 PM, Charles Hixson via Digitalmars-d-learn wrote:
It's worse than that, if they modify the length the array may be
reallocated in RAM so that the pointers held by the containing class do
not point to the changed values.  (Read the header comments...it's not
nice at all.)

Arguably, any D programmer must be aware that appending to a dynamic array potentially means making a copy of the data, and that changes to length are not visible to other views of the data.

But it's an opportunity to mess up, for sure. You could return a wrapper around the array that supports editing the data but not changing the length or appending.

Looks like std.experimental.typecons.Final [1] is supposed to be that wrapper. But in a little test I can still set the length. Not sure if that's a bug, or if Final has slightly different goals.


[1] https://dlang.org/phobos/std_experimental_typecons.html#.Final

Reply via email to