Implementing foreign traits parameterized with custom types for foreign types
I was under impression (and multidispatch RFC supports this) that it should be possible to implement operation traits for built-in types provided that trait type parameters are custom types, e.g. something like this:
impl Add<BigFloat> for f64 {
type Output = BigFloat;
...
}
However, such code does not compile:
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:312:1: 319:2 error: the type `f64` does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:312 impl Add<BigFloat> for f64 {
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:313 type Output = BigFloat;
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:314
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:315 #[inline]
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:316 fn add(self, rhs: BigFloat) -> BigFloat {
/home/dpx-infinity/dev/lang/rust/projects/mpfr.rs/src/lib.rs:317 rhs + self
I certainly remember that I was told that if one of trait parameters is "local" type, then the whole trait is considered "local", so it can be implemented for any type, not only "local" ones. Apparently this is not the case.
Is this a bug or expected behavior?
Update An issue
12 comments · 75% upvoted · other discussions
It turns out that the multidispatch RFC had a bug, in that it allowed sets of implementations that were incoherent (that is, the compiler had no way to know which impl to chose unambiguously for certain sets of types). It's really unfortunate, but (correct) compilers are hard.
See #19470 for more details. Maybe the rules can be tweaked/relaxed slightly in future.
dpx-infinity [OP] 5 points · Jan 2015
That's bad :( Overloading operators on both sides is really important, IMO.
burntsushi 3 points · Jan 2015
I think this also prevents generic conversion to
Box<error::Error>too? I had to remove impls like this from my code:japaric 1 points · Jan 2015
Yes, the pattern:
is being rejected now. If it's blocking any of your libraries, please report it in this issue