-
Notifications
You must be signed in to change notification settings - Fork 4.2k
update: clarify null-handling behavior of==operator #4808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
e50962f
8beefa7
1750bc1
b0c8e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,14 +176,15 @@ For the assignment operations, for example `a += b`, the compiler performs the f | |
| `a != b` | `!(a?.equals(b) ?: (b === null))` | | ||
|
||
These operators only work with the function [`equals(other: Any?): Boolean`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/equals.html), | ||
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. | ||
which you can override to provide a custom equality check implementation. | ||
Any other function with the same name (like `equals(other: Foo)`) is ignored. | ||
|
||
> `===` and `!==` (identity checks) are not overloadable, so no conventions exist for them. | ||
> `===` and `!==` (identity checks) aren't overloadable, so no conventions exist for them. | ||
> | ||
{style="note"} | ||
|
||
The `==` operation is special: it is translated to a complex expression that screens for `null`'s. | ||
`null == null` is always true, and `x == null` for a non-null `x` is always false and won't invoke `x.equals()`. | ||
Kotlin calls `.equals()` when neither operand is the `null` literal and the comparison isn’t between two floating-point types. | ||
Otherwise, `===` is used for `null` literal checks, and non-null floating-point values are compared by numeric value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of "the And can you make the second sentence active? "Kotlin uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🤔 It's an important difference here. With two expressions A and B, A == B expands to A?.equals(B) ?: (B === null) even with a null value. If either operand is literal
Sure thing 👍 |
||
|
||
### Comparison operators | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.