DIP 1025--Dynamic Arrays Only Shrink, Never Grow--Community Review Round 1
Ola Fosheim Grøstad
ola.fosheim.grostad at gmail.com
Mon Nov 11 18:54:04 UTC 2019
On Monday, 11 November 2019 at 17:28:09 UTC, Jonathan M Davis
wrote:
> How on earth do D dynamic arrays change their type? int[] is
> always int[].
Well, what we mean by "type" can of course be debated, but anyway:
Case 1:
char[] a = new char[2];
writeln(a.ptr);
if (false) a.length = 1;
a.length = 2;
writeln(a.ptr); // same pointer
Case 2:
char[] a = new char[2];
writeln(a.ptr);
if (true) a.length = 1;
a.length = 2;
writeln(a.ptr); // different pointer
So, whether it extends inline depends what you did with the
object dynamically. It isn't determined statically. Even though
in this case that would be possible.
Kinda like the difference between an open and closed file,
whether it is open or not depends on some dynamic external cause
(if it exists on the files system).
More information about the Digitalmars-d
mailing list