4 ms·
x == y can panic if interface values contain incomparable fields in unexported nested structs, how would I check for that? Should we let it become a query of de
by erik_seaberg 4y ago
x == y can panic if interface values contain incomparable fields in unexported nested structs, how would I check for that? Should we let it become a query of death and bet thousands of peers’ jobs on it never happening?
- ikiris 4y agodon't do that? this kind of thing is why deepcompare exists to begin with
- aetherane 4y agoIt seems unrealistic to assume that everyone on a reasonably sized team knows all of the subtle edge cases to avoid and never makes mistakes
- deleted 4y ago[deleted]
- erik_seaberg 4y agoI can nag everyone to use reflect.DeepEqual and live with some false negatives, but maps always use k1 == k2.
- Groxx 4y agoThere are also significant performance and behavior differences between the two. They are not inter-changeable, nor can one replace the other.
- ikiris 4y agomore specifically, it's really strange to hear of people doing equivalence checks on objects with structure. What are you expecting that comparison to do? I doubt it is doing what you think is happening, and is indeed risky of panics.
- mappu 4y agoThis is days later, sorry, but - you can't use an interface as a map key, so this shouldn't apply, right?
- erik_seaberg 4y agohttps://go.dev/ref/spec#Map_types https://go.dev/ref/spec#Map_types says that is allowed. > If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time panic.
- mappu 4y agoIs this really the case? Can you link to anything or an example on go.dev/play/ ? I can find a mention of "cmp.Equal" having that behavior, but that's just a third-party package panic.
- assbuttbuttass 4y agoIt's true, but you'd never really write code like this https://go.dev/play/p/r9NkQb6bQTx https://go.dev/play/p/r9NkQb6bQTx
- erik_seaberg 4y agoThe problem also affects structs that happen to have a private map or cache or callback anywhere within. https://go.dev/play/p/uP-vjpvuhku https://go.dev/play/p/uP-vjpvuhku
- preseinger 4y agoObviously `interface{}` values are not comparable?
- erik_seaberg 4y agoThe comparison is explicitly allowed in the language spec, there’s no warning for doing it, and it often works depending on the types. It’s a data-dependent runtime error, which is usually hard to guarantee test coverage for.
- preseinger 4y agoLink to an example? I don't think this is true, unless you're playing stupid games with your code, which wouldn't pass code review.