Thread overview
Cleared AA == and is have different results. Why?
2 days ago
Brother Bill
2 days ago
IchorDev
2 days ago
Brother Bill
2 days ago
Nick Treleaven
2 days ago

Page 119 of Programming in D

It seems odd that == null and is null have different values?
Is this a bug or feature?
If a feature, what is the meanings of == null vs. is null?

source/app.d

import std.stdio;

void main()
{
    // value[key]
    int[string] dayNumbers =
         //   key     : value
        [
            "Monday": 0, "Tuesday": 1, "Wednesday": 2, "Thursday": 3, "Friday": 4,
            "Saturday": 5, "Sunday": 6
        ];

    dayNumbers.remove("Tuesday");
    // writeln(dayNumbers["Tuesday"]);		// ← run-time ERROR

    dayNumbers.clear;
    writeln("dayNumbers == null? ", dayNumbers == null);

    // Better, use 'is'
    writeln("dayNumbers is null? ", dayNumbers is null);
}

Console output:

dayNumbers == null? true
dayNumbers is null? false
2 days ago

On Wednesday, 10 September 2025 at 14:01:29 UTC, Brother Bill wrote:

>

what is the meanings of == null vs. is null?

You have already asked this question here before. The spec outlines the meaning of equality and identity expressions here:

You should try to reference the spec when you don't understand something. It's essentially the best resource for learning about D's finer points.

2 days ago

On Wednesday, 10 September 2025 at 14:01:29 UTC, Brother Bill wrote:

>

Page 119 of Programming in D

It seems odd that == null and is null have different values?
Is this a bug or feature?
If a feature, what is the meanings of == null vs. is null?

Feature. An AA has reference semantics. Removing all elements does not set it to null (the .init value), because it is already allocated, which may be useful for future operations.

Comparing for equality against null is true because both AA references have no elements (null is a valid AA reference).

Comparing for identity is false because null has no AA allocation whereas dayNumbers does - it isn't null.

https://dlang.org/spec/hash-map.html#construction_and_ref_semantic

2 days ago

On Wednesday, 10 September 2025 at 14:25:05 UTC, IchorDev wrote:

>

On Wednesday, 10 September 2025 at 14:01:29 UTC, Brother Bill wrote:

>

what is the meanings of == null vs. is null?

You have already asked this question here before. The spec outlines the meaning of equality and identity expressions here:

You should try to reference the spec when you don't understand something. It's essentially the best resource for learning about D's finer points.

Thank you so much. Got it.