Hacker News new | past | comments | ask | show | jobs | submit login

You're missing the distinction between isomorphic and identical.

Your two types are isomorphic. They are not equal.




Isomorphism means having the same “interface.” Unless you specify, for each structure, a different set of operations (functions), what’s left is their, well, structures, which in this case are identical.


The problem with isomorphism is that there are many ways two types can be considered isomorphic.

For example, { x: int, y: int } and { a: int, b: int } are isomorphic, but in two different ways: one that assigns x to a and y to b, and another that assigns x to b and y to a.

The case where each type has identical field names ({ x: int, y: int } and { x: int, y: int }) induces an structural equality that is the isomorphism that you expect, but there's also another that swaps x and y. (I think that "structural equality" is what you want, after all)

Structural typing (and even better, row typing) is nice for specifying ad-hoc APIs but they don't scale much. In the general case, when you have two types that refer to the same thing but were written by two entirely different teams, it will be very rare for them to be structurally identical even in field names.

In those cases it's better to (also) specify a general facility for converting between types, like Rust's From and Into traits, and a facility to auto-derive them in simple cases

https://doc.rust-lang.org/rust-by-example/conversion/from_in...


Admittedly it's up to the language whether those two types should be considered identical or not, but there's nothing wrong with considering them unequal.


Yes, as long as there is a particular intent behind each structure even if their “form” is the same. Simply giving different names (to the structures themselves and, especially, to their respective fields) might hint to that. (The field names, in particular, should be considered part of the “interface,” actually.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: