ref semantics with hashes

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed May 2 15:38:52 PDT 2012


On 5/2/12, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> The way that AAs work in this regard is identical to how dynamic arrays
> work.

Yes but they're very different in other regards. For example, if you
add an element to one array it doesn't automatically add an element to
the other array:

    int[] a = new int[](2);
    auto b = a;
    b ~= 1;
    a ~= 2;  // boom, a is now a new array

    writeln(a);  // [0, 0, 2]
    writeln(b);  // [0, 0, 1]

This is different from hashes, where adding a new element doesn't
magically create a new hash:

    int[int] ha = [0:0];
    auto hb = ha;
    hb[1] = 1;
    writeln(ha);  // [0:0, 1:1]
    writeln(hb);  // [0:0, 1:1]

But maybe this is more relevant to how nulls and references work
rather than anything about hashes.

In fact what would *actually* solve this problem (for me) is if/when
we have aliases implemented for this scenario:

import std.stdio;

struct Foo
{
   int[] hash;
}

void main()
{
    Foo foo;
    alias foo.hash hash2;

    hash2[0] = 1;
    writeln(foo.hash);  // [1:1]
    writeln(hash2);     // [1:1]
}

Hopefully we get that one day.


More information about the Digitalmars-d-learn mailing list