3 ms·
It's interesting that this is one of the things that Go and Rust have agreed upon: that the addresses of zero-sized values are the same (in Go we spell a zero-s
by dsymonds 12y ago
It's interesting that this is one of the things that Go and Rust have agreed upon: that the addresses of zero-sized values are the same (in Go we spell a zero-sized type as "struct{}").
It has some unexpected surprises though. If you write `var key struct{}` in a package and then try to use &key as a map key (c.f. http://blog.golang.org/context http://blog.golang.org/context) then you'll collide with other packages. You've got to use a non-zero type instead.
- pcwalton 12y agoI don't think we actually defined the addresses of zero-sized values to be equal; I believe it's actually unspecified. I don't think this has ever come up in practice, though; we could define them to be equal if we wanted to. That issue hasn't come up in Rust, probably because equality and hashing look through references (per the definitions of Hash and Eq in the standard library), so you'd really have to go out of your way to try to use unit as a way to make package-specific keys like that.