Associated Arrays and null. dict == null vs. dict is null

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 3 15:51:37 UTC 2025


On Saturday, 1 November 2025 at 12:23:07 UTC, Brother Bill wrote:
> What does dict == null mean vs dict is null, when dict is a 
> dictionary?
>
> Is this table accurate?
> ```
>  dict == null    dict is null    # elements   memory allocated
>       true           true            0             no
>       true           false           0             yes
>       false          false           1+            yes
>       false          true     (Is this a possibility, and if 
> so, how to create it?)
> ```

The last one isn't possible.

To explain, an Associative Array is a struct that is a single 
pointer to the actual implementation. The implementation is an 
internal structure that stores all the data about the aa.

When the aa is null, this means it is a null pointer. This is 
treated equivalently to an allocated aa that is empty.

Similar to arrays, `==` means, compare the *contents* of the aa. 
This means that even if the buckets are not in the same order 
(due to insertions, collisions, etc), you will get a comparison 
of the keys and values, independent of ordering.

This also means that if you use `== null`, it will compare as 
equal if the aa is empty, regardless of whether it is allocated 
or not.

Whereas, `is` means, "is this the exact same instance". I.e. is 
this the same pointer.

Note: you can allocate an empty AA by using the `new int[string]` 
syntax.

-Steve


More information about the Digitalmars-d-learn mailing list