On Sunday, 19 October 2025 at 21:01:06 UTC, monkyyy wrote:

Appending one slice sets the capacity to zero on other related slices; its best to treat slices as dynamic arrays or slices, not both. It will just be spooki action at a distance.

I'm missing something.

Step 1 was create a dynamic array named slice with initial values.
At this point, slice does not have slice semantics.

Step 2 was to append a new element.
Does this convert slice into an actual slice, and if so, why?

I simplified the program, removing the part of extending slice.
It still breaks slice sharing...

```
void main()
{
        int[] slice = [1, 3, 5, 7, 9, 11, 13, 15];
writeln("slice: ", slice, " length: ", slice.length, " capacity: ", slice.capacity);
        writeln();

        int[] tailSlice = slice[$ / 2 .. $];
writeln("slice: ", slice, " length: ", slice.length, " capacity: ", slice.capacity); writeln("tailSlice: ", tailSlice, " length: ", tailSlice.length, " capacity: ", tailSlice.capacity, " &tailSlice[0]: ", &tailSlice[0]);
        tailSlice.length += 1; // fails here
        // tailSlice ~= 0; // also fails
writeln("tailSlice after incrementing length by 1: ", tailSlice, " length: ", tailSlice.length, " capacity: ", tailSlice.capacity, " &tailSlice[0]: ", &tailSlice[0]);
        tailSlice[0] = 888;
writeln("After tail slice length increase and changing tailSlice[0] to 888. ", " length: ", tailSlice.length, " capacity: ", tailSlice.capacity);
        writeln("tailSlice: ", tailSlice);
        writeln("slice    : ", slice);
}
```

Console output:
```
slice: [1, 3, 5, 7, 9, 11, 13, 15] length: 8 capacity: 11

slice: [1, 3, 5, 7, 9, 11, 13, 15] length: 8 capacity: 11
tailSlice: [9, 11, 13, 15] length: 4 capacity: 7 &tailSlice[0]: 1E9ECD71010 tailSlice after incrementing length by 1: [9, 11, 13, 15, 0] length: 5 capacity: 7 &tailSlice[0]: 1E9ECD70020 After tail slice length increase and changing tailSlice[0] to 888. length: 5 capacity: 7
tailSlice: [888, 11, 13, 15, 0]
slice    : [1, 3, 5, 7, 9, 11, 13, 15]
```

Reply via email to