3 ms·
Some Rust libraries use a trick to make sum types extensible without introducing breaking changes when a new invariant is added. There is a private/undocumented
by hlasdjlfhalwjk 6y ago
Some Rust libraries use a trick to make sum types extensible without introducing breaking changes when a new invariant is added. There is a private/undocumented invariant in the type so every pattern match over the type must have a default/catch-all/wildcard branch (e.g. it is not possible to do an exhaustive match by naming all existing variants). By having a default branch, the match is always exhaustive and there will be no error if a new variant is added. For fields in a sum type something similar can be done: an undocumented, hidden field that cannot be used when the variant is destructed. Therefore it can be only partially destructed and in this case it doesn't matter how many other fields there are in the type variant.
Here is an example: https://github.com/rust-lang/regex/blob/master/regex-syntax/src/ast/mod.rs#L177 https://github.com/rust-lang/regex/blob/master/regex-syntax/...
- ori_b 6y agoForcing a default case is a big step back towards dynamic typing.
- heavenlyblue 6y ago“Hey - we have a solution to your type problem; we just disabled type checking. Genious”