From 4065469c290df053e1b3e9a0b31fbfc043dba142 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Tue, 21 Feb 2017 08:41:44 +0100 Subject: [PATCH] mention identity comparison - fixes #30 (#69) * mention identity comparison - fixes #30 * fix identity for other types --- basics/classes.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/basics/classes.md b/basics/classes.md index 16a0d97..a0870be 100644 --- a/basics/classes.md +++ b/basics/classes.md @@ -41,6 +41,23 @@ it that it isn't instantiated - `super(..)` can be used to explicitly call the base constructor +### Checking for identity + +For class objects, the `==` and `!=` operators compare the contents of the objects. +Therefore, comparing against `null` is invalid, as `null` has no contents. +The `is` compares for identity. To compare for nonidentity, use `e1 !is e2`. + +```d +MyClass c; +if (c == null) // error + ... +if (c is null) // ok + ... +``` + +For `struct` objects all bits are compared, +for other operand types, identity is the same as equality. + ### In-depth - [Classes in _Programming in D_](http://ddili.org/ders/d.en/class.html)