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

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...




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

Search: