So, I’d like to propose a feature. Right now, if you define a struct/enum locally, you can define methods on it very easily just be writing impl MyType { ... }. Unfortunately, you can only do this for things defined in the current crate. The reason is coherence – if we allowed other crates to define inherent methods, we couldn’t check for conflicts. So you might have two crates that define a method draw(), and then it’d be unclear which one you are invoking.
I was thinking today that it would be cool to allow you to define inherent methods in other crates too, but only if they are restrcted to the current crate. We didn’t used to have a way to do that, but we do now: pub(crate). So you could imagine allowing inherent methods to be defined on foreign types, as long as they are at most pub(crate).
Some problems I can imagine:
- What if there is a conflict with the base crate? Unlike with traits, we have no way to disambiguate which one you wanted. Probably this is just an error and you have to rename your method locally.
- Problem with that is that it would imply that adding a new inherent method is potentially a breaking change for your users, since they might have done the same.
- So maybe your local methods take precedence?
- That would create a copy-and-paste hazard, but I’m not sure if that’s an issue.
- Or else we come up with a way to say which one you want in a UFCS-style; still a breaking change, but closer to overlap with traits.
- So maybe your local methods take precedence?
- Problem with that is that it would imply that adding a new inherent method is potentially a breaking change for your users, since they might have done the same.
Anyway, what do people think?
