Empty associative array

jfondren julian.fondren at gmail.com
Mon Aug 9 12:46:28 UTC 2021


On Monday, 9 August 2021 at 12:16:44 UTC, deadalnix wrote:
> In D associative array have reference semantic. Well they do, 
> except when they are initially created, in which case they are 
> null. For instance:
>
> ```d
> int[int] a1;
> int[int] a2 = a1; // a1 is null, so a2 is null too.
>
> a2[3] = 3; // a2 is initialized to something else than null 
> here.
> writeln(a1.length); // prints 0.
> ```

```d
class Cache {
     int[int] aa;
}

unittest {
     auto a1 = new Cache;
     auto a2 = a1;

     a2.aa[3] = 3;
     assert(a1.aa.length == 1);
}
```

> I find myself in a situation where I use an AA to cache some 
> computation results, and this cache might be necessary in a 
> couple of places in the application.

You want reference semantics (class) and an AA (put it in the 
class).




More information about the Digitalmars-d mailing list