Thinking about the difference between fixed and 'dynamic' arrays.

Siarhei Siamashka siarhei.siamashka at gmail.com
Wed Nov 30 01:53:10 UTC 2022


On Wednesday, 30 November 2022 at 00:40:57 UTC, Vladimir 
Panteleev wrote:
> On Tuesday, 29 November 2022 at 18:59:46 UTC, DLearner wrote:
>> Suggestion: it would be clearer if the two concepts were 
>> separated:
>> 1. Convert 'int[] VarArr;' so it produces a straightforward 
>> _value-type_ variable array, called 'VarArr';
>> 2. Implement a new concept 'int slice Window;' to produce an 
>> object of type 'int slice', called 'Window'.
>>    'Window' is a 'slice' into an int array, not an array 
>> itself or even a variable.
>>
>> Opinions?
>
> Yes, that's what Rust does. It has first-class variable-size 
> value types, D doesn't have such a feature.

I'm not really familiar with Rust, but it also seems to have the 
concept of either making a full copy or creating a slice with a 
view into the existing array. Just the default assignment via 
`"let c = a;"` creates a copy. While creating a slice needs a 
more elaborate explicit syntax. Rust also appears to be picky 
about the order of operations:

```Rust
fn main() {
     let mut a = [1, 2, 3, 4, 5];
     let c = a;
     let b = &mut a;

     b[1] = 99;

     println!("{:?}", b); // [1, 99, 3, 4, 5]
     println!("{:?}", a); // [1, 99, 3, 4, 5]
     println!("{:?}", c); // [1, 2, 3, 4, 5]
}
```

> It is too late to change it in D, nor is it often useful in 
> practice.

If this is really desired, then the D compiler can probably 
introduce a more verbose syntax for shallow array copies and 
start spitting out warnings about simple assignments like `"auto 
b = a;"`. A few years later the old syntax can be dropped.

```D
import std;

void main() {
   auto a = [1, 2, 3, 4, 5];
   auto b = a.slice; // Not supported right now, but maybe is more 
readable?
   auto c = a.dup;

   a[1] = 99;

   writeln(a); // [1, 99, 3, 4, 5]
   writeln(b); // [1, 99, 3, 4, 5]
   writeln(c); // [1, 2, 3, 4, 5]
}
```

But way too many languages behave in the same way as D right now. 
I personally don't see any problem.


More information about the Digitalmars-d-learn mailing list