NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
The State of Coherence in the Land of Type Classes (programming-journal.org)
maybevoid 4 days ago [-]
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!

ratmice 4 days ago [-]
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.

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 16:08:08 GMT+0000 (Coordinated Universal Time) with Vercel.