by adamnemecek · 8y ago · 0 comments
This is kinda funny.
E.g. you could on a 32-bit machine reserve the least significant bit to mark an integer, and if it is set the high 31 bits are treated as a 31-bit integer instead of a pointer. Since your objects are likely bigger than a byte, you don't actually need all 32 bits for addresses and thus don't even loose addressable memory space. On 64bit machines you can do it even more, since they are far from being able to use all bits for actual memory, and you might even fit a short string or other more complex type in there.
On the other hand if you want to check if two integers are different, why would you check if they are different objects, after all they could still represent the same numerical value.
I mean I get that this is something that probably bites someone at the worst possible moment, but isn't this the case for most optimizations?
a = integer, b = same integer
a is b
is always false.The inconsistency is that the two separate variables reference the same object just because the values are the same.
In general, you shouldn't care about identity (is), just about value comparison (==).