T[new] misgivings

Don nospam at nospam.com
Fri Oct 16 00:28:26 PDT 2009


Andrei Alexandrescu wrote:
> I talked to Walter about T[new] today and it seems we are having a 
> disagreement.
> 
> The problem is that I believe T[new] is a container, whereas Walter 
> believes T[new] is nothing but a slice with a couple of extra operations.
> 
> Paradoxically this seems to be conducive to subtle efficiency issues. 
> For example, consider:
> 
> int[new] a;
> ...
> a = [1, 2, 3];
> 
> What should that do?

If we made array literals immutable, it'd be obvious.

There are two sensible options:
(1) An error. a = [1, 2, 3].dup; should have the semantics Walter describes.
(2) Be equivalent to a.length = 3; a[] = [1,2,3]; (Andrei semantics)

But in case (2),  char[new] x = "abc"; should also compile (without a .dup).

But I don't understand how the whole thing works.

int[new] a = [1,2,3,4].dup;
int[] b = a[0..3];
a.length = 1;
int c = b[2];

How can this be legal in Safe D ?
Without reference counting, the only option I can think of is to make it 
illegal to reduce the length of a T[new] array: you need to reallocate 
if you want to shrink it.



More information about the Digitalmars-d mailing list