www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cleared AA == and is have different results. Why?

reply Brother Bill <brotherbill mail.com> writes:
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
```
Sep 10
next sibling parent reply IchorDev <zxinsworld gmail.com> writes:
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](https://forum.dlang.org/thread/nvaiwzvcrahnwziorazc forum.dlang.org). The spec outlines the meaning of equality and identity expressions here: - https://dlang.org/spec/expression.html#equality_expressions - https://dlang.org/spec/expression.html#identity_expressions 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.
Sep 10
parent Brother Bill <brotherbill mail.com> writes:
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](https://forum.dlang.org/thread/nvaiwzvcrahnwziorazc forum.dlang.org). The spec outlines the meaning of equality and identity expressions here: - https://dlang.org/spec/expression.html#equality_expressions - https://dlang.org/spec/expression.html#identity_expressions 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.
Sep 10
prev sibling parent Nick Treleaven <nick geany.org> writes:
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
Sep 10