Skip to content

Commit e2a1801

Browse files
authored
update: clarify null-handling behavior of==operator (#4808)
* update: clarify null-handling behavior of==operator * update: implementing comments from Nikita * update: implementing TWr review comments * Implementing comments from Sarah
1 parent 9092987 commit e2a1801

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

docs/topics/operator-overloading.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,16 @@ For the assignment operations, for example `a += b`, the compiler performs the f
176176
| `a != b` | `!(a?.equals(b) ?: (b === null))` |
177177

178178
These operators only work with the function [`equals(other: Any?): Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/equals.html),
179-
which can be overridden to provide custom equality check implementation. Any other function with the same name (like `equals(other: Foo)`) will not be called.
179+
which you can override to provide a custom equality check implementation.
180+
Any other function with the same name (like `equals(other: Foo)`) is ignored.
180181

181-
> `===` and `!==` (identity checks) are not overloadable, so no conventions exist for them.
182+
Kotlin calls `.equals()` when neither operand compares directly to `null` in the `==` expression and the comparison isn't between two floating-point types.
183+
Otherwise, Kotlin uses `===` for direct `null` comparisons and compares non-null floating-point values by numeric value.
184+
185+
> `===` and `!==` (identity checks) aren't overloadable, so no conventions exist for them.
182186
>
183187
{style="note"}
184188

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()`.
187-
188189
### Comparison operators
189190

190191
| Expression | Translated to |

0 commit comments

Comments
 (0)