Shameless plug: I am working on a solution in Rust that solves the coherence problems discussed in this post. I call it Context-Generic Programming (CGP), which makes it possible to write modular code that is generic over a context (Self) type without the coherence restrictions.
The basic idea of CGP is pretty simple: we introduce a companion provider trait that has an additional generic parameter that is placed at the "Self" position of the trait, and define a unique provider type that identifies each of the generic implementation. CGP then makes use of blanket implementations and macros to enable wiring of generic providers to a concrete context to implement the original trait, which we now call a consumer trait.
Currently, I am preparing for a new v0.4.0 release of CGP, which includes a lot of quality of life improvements, including significantly improved error messages. I will also write a follow up blog post to specifically address how CGP can be used to solve the coherence problems stated in this post. So do stay tuned for more updates on CGP!
I know there is definitely some wonkyness with rust traits and type inference,
// Uncomment for compiler error.
// use serde_json as _;
//
// Because serde_json includes impl PartialEq<serde_json::value> for u32
let _ = 1u32 == 1usize.try_into().unwrap();
One really interesting thing is that this can affect a comparison in a totally separate crate that never uses serde_json via features.
Shameless plug: I am working on a solution in Rust that solves the coherence problems discussed in this post. I call it Context-Generic Programming (CGP), which makes it possible to write modular code that is generic over a context (Self) type without the coherence restrictions.
The basic idea of CGP is pretty simple: we introduce a companion provider trait that has an additional generic parameter that is placed at the "Self" position of the trait, and define a unique provider type that identifies each of the generic implementation. CGP then makes use of blanket implementations and macros to enable wiring of generic providers to a concrete context to implement the original trait, which we now call a consumer trait.
You can read about the full details of CGP at https://contextgeneric.dev/. I am also writing a book for CGP at https://patterns.contextgeneric.dev/, which goes more into details on how it works behind the scene.
Currently, I am preparing for a new v0.4.0 release of CGP, which includes a lot of quality of life improvements, including significantly improved error messages. I will also write a follow up blog post to specifically address how CGP can be used to solve the coherence problems stated in this post. So do stay tuned for more updates on CGP!
I know there is definitely some wonkyness with rust traits and type inference,
One really interesting thing is that this can affect a comparison in a totally separate crate that never uses serde_json via features.https://play.rust-lang.org/?version=stable&mode=debug&editio...