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

Vladimir Panteleev thecybershadow.lists at gmail.com
Wed Nov 30 02:05:07 UTC 2022


On Wednesday, 30 November 2022 at 01:53:10 UTC, Siarhei Siamashka 
wrote:


> 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]
> }
> ```

This seems unsurprising to me, `b` is a slice. The same in D:

```d
import std.array, std.stdio;

void main()
{
     version (dynamic)
     {
         auto a = [1, 2, 3, 4, 5];
         auto c = a.dup;
         auto b = a;
     }
     else
     {
         auto a = [1, 2, 3, 4, 5].staticArray;
         auto c = a;
         auto b = a[];
     }

     b[1] = 99;

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

I agree the syntax is inconsistent.

>> 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.

I only meant that variable-sized value types are not often useful 
in practice.

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

Slices are such a fundamental feature of D that it is unrealistic 
to think about changing syntax there. It would effectively be a 
new language, because almost no programs from before the change 
would compile after the change.



More information about the Digitalmars-d-learn mailing list