Consistent set of operations for equality and identity comparison (reformatted)
Simon Ochsenreither
simon at ochsenreither.de
Tue Mar 27 15:12:22 UTC 2018
Apologies everyone, the tables ended up being garbled. I reformatted them to fit
into 72 columns:
========================================
Hi everyone,
are there any plans to have comparison operations for identity¹ and equality
consistently defined across all types?
As far as I have seen there are some ideas around acmp and substitutableEquals,
but they seem to be rather limited from my understanding.
Currently, the definition of equality and identity in Java is all over the
board:
| | int/long/…² | float/double | Refs. | Arrays | null |
|--------| ----------- | ------------- | ------ | ---------- | ------ |
|Identity| == | box(x).equals | == | ==/equals | == |
|Equality| == | == | equals | deepEquals | == |
I believe that having a consistent set of such operations would not only be
extremely helpful for Valhalla, but also for the extended switch construct and
the idea of data/record/case classes.³
Which brings me back to the question – is there anything planned (either as a
bytecode, or as a method in the JDK) that does something akin to this:
| | int/long/…² | float/double | Refs. | Arrays | null |
|--------| ----------- | ------------- | ------ | ---------- | ------ |
|Identity| id_cmp | id_cmp | id_cmp | id_cmp | id_cmp |
|Equality| eq_cmp | eq_cmp | eq_cmp | eq_cmp | eq_cmp |
Basically,
- id_op, "is A identical to B?":
- compares the value at hand (be it a reference or primitive) based on "the
bits" (+ type, implied for references)
-> roughly `acmp` (reference types) + ??? (for value types)
- eq_op, "is A equal to B?":
- compares the value based on the library-defined notion of equality
(overridden equals)
-> roughly `invoke equals` + the correct definition of equals for float and
double, such that NaN eq_op NaN == false + doesn't throw on nulls
Thanks,
Simon
¹ Identity seems to be called "substitutability" on this list, so please read
substitutability if "identity" causes confusion.
² Identity and equality have the exact same semantics on integral types.
³
http://mail.openjdk.java.net/pipermail/amber-spec-observers/2017-December/000250.html
More information about the valhalla-dev
mailing list