On 2016-03-02 20:42, Ozan wrote:
Hi

I despair of "auto var1 = var2"for arrays. Isn't it a open door for
errors. Example

import std.stdio;

void main()
{
     int[] a;
     foreach(i; 0..10) a ~= i;
     auto b = a; // correct dlang coding: auto b = a.dup;

     a[2] = 1;
     b[2] = 5; // Overwrites assignment before
     writeln(a);
     writeln(b); // Always a == b but developer would like to have (a != b)
}

The behaviour is different to other non-container datatypes.
So in a first view, it looks like a data copy but it's only a pointer copy.

Depending on your needs you can make the array const, or immutable:

void main()
{
    const(int)[] a;
    foreach(i; 0..10) a ~= i;
    auto b = a;

    a[2] = 1; // does not compile
    b[2] = 5; // does not compile
}

--
/Jacob Carlborg

Reply via email to