Separate slices and dynamic arrays
DLearner
bmqazwsx123 at gmail.com
Tue Jan 28 15:32:38 UTC 2025
On Wednesday, 31 July 2024 at 23:07:14 UTC, monkyyy wrote:
> ```d
> int[3] foo;
> int[] bar=foo[];
> bar[1]=3;
> assert(foo[1]==3);//works, bar is a reference to foo
> bar~=5;//oh no
> bar[2]=4;
> assert(foo[2]==4);//failure, bar stopped being a reference to
> foo
> ```
>
> slices are sometimes better pointer math or sometimes gc'ed
> managed memory. This is unsafe and lacks clear ownership.
>
> So two parts:
>
> Introduce `int[?]` as a dynamic array, it "owns" the memory and
> duplicates on assignment from slices.
>
> Deprecate append to slices.
>
> `auto bar=foo[];` would still be a slice
Suggest calling the int[?] a 'variable length array', rather than
a 'dynamic array'.
What (I think) you are proposing is that T[?] defines a variable
length version of a fixed array, preserving the fixed array's
attribute of being a value type.
IMO this is a very good thing; the current situation with
'dynamic' arrays being a reference type may be well-defined, but
(speaking personally) I find the behaviour of dynamic arrays less
than obvious.
More information about the dip.ideas
mailing list