|
|
|
|
|
by irogers
1329 days ago
|
|
People try to get cute with 3-way compares. A real Java bug that inspired:
https://errorprone.info/bugpattern/BadComparable public MyFile implements Comparable {
...
long timestamp;
...
@Override
public int compare(Object other) {
return (int)(((MyFile)other).timestamp - timestamp;
}
...
} The int conversion loses the sign of the subtract. The particular use of the code was sorting files to delete the X number of oldest. More examples about the dangers with just ints:
https://stackoverflow.com/questions/2728793/java-integer-com... The comparable API in Java came from the C qsort API. It would have been better to just have a less-than method. Kind of surprised to see this in Go near a core API. |
|