News for hackers. Stuff that matters.
Re:Go 1.21 may have a clear(x) builtin by squeaky-clean on Saturday November 19, 2022 @07:49PM (#33676840)
The Java (openjdk) hashmap putValue doesn't seem to compare types, it just uses .equals()

  if (p.hash == hash &&
                  ((k = p.key) == key || (key != null && key.equals(k))))
You can't really rely on checking types in a Hashmap anyways because the type you're hashing can have more possible values than the 32bit hash. For example you can have 2 strings that have a hash collision.

NaN's work in a Java hashmap because Float.hashCode returns floatToIntBits(float) which normalizes all NaN's to a canonical value.

Re:Go 1.21 may have a clear(x) builtin by tialaramex on Sunday November 20, 2022 @08:52AM (#33680867)
Sorry, I should have said compare values of-this-type. I was focused on the fact that type does matter here, which as you show the equals method is called on key, not some arbitrary system-wide equals method but a method defined on key's type.

Rust has a magic unstable trait StructuralEq which means not only are we promised that values of this type can be compared for equality (that's what Eq does) but that comparison will just be a bitwise memory comparison. Lots of useful things are not StructuralEq even if they are Eq

Apparently Java's Floats also do the same canonicalisation for equals(). So in effect in Java although NaN != NaN, once you box it that ceases to be true. This seems like a spectacularly bad idea to me, but presumably it made somebody's awful hack work at one point.

Re:Go 1.21 may have a clear(x) builtin by senknvd on Sunday November 20, 2022 @04:41PM (#33685690)
> This seems like a spectacularly bad idea to me, but presumably it made somebody's awful hack work at one point.

but elsewhere you say, "You are welcome to build a type which has this property and declares that it is Eq". This is exactly the difference between double and Double in Java. double has the `==` operator that isn't reflexive and works as required by IEEE-754 and Double has Double.equals, which is documented[1] to be a reflexive, transitive, symmetric, consistent, and substitutable relation.

[1]: https://docs.oracle.com/en/java/javase/17/docs/api/java.base...

Re:Go 1.21 may have a clear(x) builtin by tialaramex on Tuesday November 22, 2022 @04:12AM (#33703705)
Is it clearer if I explain that you are welcome to do things which are a spectacularly bad idea ?