4

I just came across the problem of implementing a trait I do not own for a type I do not own. Then I googled the exact How do I implement a trait I don't own for a type I don't own? question.

The thing that confused me is the motivation behind such restriction. I came from Scala where it is possible to have an external typeclass instance for an external type.

Why does Rust restrict that?

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
Some Name
  • 7,566
  • 4
  • 17
  • 50
  • 4
    Opinion based, but suppose an occasion when crate A and crate B both make implementations of a trait `C::foo` for type `D::Bar`, which to choose? – Alexey Larionov Jul 25 '20 at 20:51
  • @AlexLarionov Makes sense. – Some Name Jul 25 '20 at 21:25
  • See also [Why is implementing an external trait using my type as a parameter for an external type legal?](https://stackoverflow.com/q/51236531/155423); [Is there a way for me to use #\[derive\] on a struct or enum from a library without editing the actual library's source code?](https://stackoverflow.com/q/48267016/155423) – Shepmaster Jul 29 '20 at 13:01
  • As far as I remember, some people involved in the design of Rust have a really bad experience with this from Haskell. In Haskell you can define existing typeclasses for existing types and some packages do it and it leads to difficult to understand bugs and errors. – Jan Hudec Jul 29 '20 at 13:55

1 Answers1

1

I just read the Rust Book's chapter about implementing traits and, as @AlexLarionov suggested in the comment that it would be impossible to choose an appropriate implementation:

But we can’t implement external traits on external types. For example, we can’t implement the Display trait on Vec<T> within our aggregator crate, because Display and Vec<T> are defined in the standard library and aren’t local to our aggregator crate. This restriction is part of a property of programs called coherence, and more specifically the orphan rule, so named because the parent type is not present. This rule ensures that other people’s code can’t break your code and vice versa. Without the rule, two crates could implement the same trait for the same type, and Rust wouldn’t know which implementation to use.

Some Name
  • 7,566
  • 4
  • 17
  • 50