Thinking about the difference between fixed and 'dynamic' arrays.
Siarhei Siamashka
siarhei.siamashka at gmail.com
Wed Nov 30 00:52:51 UTC 2022
On Tuesday, 29 November 2022 at 23:25:46 UTC, DLearner wrote:
> Many languages also have variable length arrays, I suggest D's
> 'dynamic array' _does not_ operate as expected.
> I'm not suggesting that the result contradicts D's definition
> of 'dynamic array', nor it's implementation, just that 'dynamic
> array' is not a reasonable description for a construct that
> behaves like VarArr2[3] becoming 40.
Which programming languages set your expectations this way? Many
programming languages have the concept of "deep" vs. "shallow"
copy. D is a part of a big crowd:
D:
```D
import std;
void main()
{
auto a = [1, 2, 3, 4, 5];
auto b = a;
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]
}
```
Python:
```Python
a = [1, 2, 3, 4, 5]
b = a
c = a.copy()
a[1] = 99
print(a) # [1, 99, 3, 4, 5]
print(b) # [1, 99, 3, 4, 5]
print(c) # [1, 2, 3, 4, 5]
```
Ruby/Crystal:
```Ruby
a = [1, 2, 3, 4, 5]
b = a
c = a.dup
a[1] = 99
pp a # [1, 99, 3, 4, 5]
pp b # [1, 99, 3, 4, 5]
pp c # [1, 2, 3, 4, 5]
```
Kotlin:
```Kotlin
fun main() {
var a = intArrayOf(1, 2, 3, 4, 5)
var b = a
var c = a.copyOf()
a[1] = 99
println(a.contentToString()) // [1, 99, 3, 4, 5]
println(b.contentToString()) // [1, 99, 3, 4, 5]
println(c.contentToString()) // [1, 2, 3, 4, 5]
}
```
I could list even more languages.
More information about the Digitalmars-d-learn
mailing list