groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dario Bertini (JIRA)" <>
Subject [jira] [Commented] (GROOVY-7914) Comparison operator does not delegate to .equals() for null
Date Thu, 25 Aug 2016 08:57:20 GMT


Dario Bertini commented on GROOVY-7914:

You're obviously free to decide not to fix this, but there are several reasons to fix/reconsider
this behavior:

- Groovy's semantics are different from Java's (obviously), Java defines .equals as implementing
an "equivalence relation on non-null object references". Which is a cop-out to justify a NPE
when `a.equals(b)` because `a` is `null`. Groovy does not have a special behavior for null:
its own null has a type with a name (unlike Java) and `null.equals(null)` works just fine
in Groovy.
- In no place is Groovy's `==` operator documented to have a different behavior than `.equals`.
- Worrying about performance without any benchmark to demonstrate the problem in the real
world is premature optimization
- Not fixing this, would prioritize performance over correctness
- Every other dynamically typed language worth its salt avoids special casing this (I confirmed
with Python, Ruby, Clojure...)
- If people want to check referece identity rather than equality (for performance reasons
or whatever other reason), they can (and should be taught to) be explicit, by doing `
- Principle of least surprise

> Comparison operator does not delegate to .equals() for null
> -----------------------------------------------------------
>                 Key: GROOVY-7914
>                 URL:
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Dario Bertini
> This might be related to
> supposedly a PR over there has been merged, but the bug is still flagged as open (and
indeed, I can locally reproduce it).
> This one instead caused problems when handling org.json.JSONObject
> That type uses its own internal JSONObject.Null class to represent null inside json (which
is useful to distinguish between keys being missing, and values explicitly being set to null)
> it overrides .equals(other) to check if the other object is null, but that method is
never invoked
> basically:
> JSONObject.NULL == null
> false
> JSONObject.NULL.equals(null)
> true

This message was sent by Atlassian JIRA

View raw message