Skip to content

Commit e50962f

Browse files
committed
update: clarify null-handling behavior of==operator
1 parent 6c8fe3e commit e50962f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

docs/topics/operator-overloading.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ which can be overridden to provide custom equality check implementation. Any oth
182182
>
183183
{style="note"}
184184

185-
The `==` operation is special: it is translated to a complex expression that screens for `null`'s.
186-
`null == null` is always true, and `x == null` for a non-null `x` is always false and won't invoke `x.equals()`.
185+
The `==` operation translates `a == b` to `a?.equals(b) ?: (b === null)`, which safely handles comparisons involving `null` values.
186+
The behavior of the comparison depends on the nullability of the values being compared:
187+
188+
* If `a` is `null`, the comparison returns `true` only if `b` is also `null`, and no function is called.
189+
* If `a` is a nullable type and isn't `null` at runtime, the compiler calls `a.equals(b)`, even if `b` is `null`.
190+
* If `a` is a non-nullable type and `b` is known to be `null`, the compiler replaces the comparison with `false` at compile time and skips the function call.
187191

188192
### Comparison operators
189193

0 commit comments

Comments
 (0)