0

Here is a toy example to reproduce my problem.

I want to create a different impl of Splash according to whether a Puddle is Clear or Muddy.

The compiler tells me that my Muddy Splash and Clear Splash conflict.

However there is no way I can find (so far) to specify a type P that implements Puddle with both Puddle::Purity=Muddy and Puddle::Purity=Clear. The ExamplePuddle also fails to compile with conflicting impls of Puddle.

So if I can't implement multiple different Puddle::Purity for any type P, then why can't I implement different Splash for different Puddle::Purity constraints?

trait WaterPurity {}

struct Muddy;
impl WaterPurity for Muddy {}

struct Clear;
impl WaterPurity for Clear {}



trait Puddle {
    type Purity: WaterPurity;
}

trait Splash {}
struct Foot;


// Conflicting implementations of Splash for type (_, Foot)
impl<P> Splash for (P, Foot) where
    P: Puddle<Purity=Muddy> {}
impl<P> Splash for (P, Foot) where
    P: Puddle<Purity=Clear> {}


struct ExamplePuddle;

// Conflicting implementations of trait Puddle for ExamplePuddle
impl Puddle for ExamplePuddle {
    type Purity = Muddy;
}
impl Puddle for ExamplePuddle {
    type Purity = Clear;
}
World Outsider
  • 287
  • 2
  • 7
  • 1
    Note that this is likely not a feasible design decision either way. The part about blanket implementations may be a limitation of the compiler, but it is still illegal to `impl Puddle for ExamplePuddle` twice like that, by design. – E_net4 Oct 12 '21 at 10:23
  • if you pay attention I put down the `impl Puddle for ExamplePuddle` to demonstrate that it is not possible to write these conflicting Puddles, therefore the `impl

    Splash` constrained by `Puddle::Purity` should not be considered conflicting. I don't care about writing the multiple Puddle impls, I want multiple Splashes, but the compiler error for the multiple Splashes implies the multiple Puddles would be possible when clearly it is not.

    – World Outsider Oct 13 '21 at 13:26

1 Answers1

-2

This is an open issue. According to comments in the issue, specialization may be a work around.

World Outsider
  • 287
  • 2
  • 7
  • The code in your question is just plain wrong, and the open issue being fixed wouldn't make it work. Take note of the `` part in `impl AdditiveIterator for I where I: Iterator {`: you have no equivalent to that: `impl Puddle for ExamplePuddle {` in both. – Joseph Sible-Reinstate Monica Oct 12 '21 at 21:20
  • if you pay attention, I'm not proposing that the multiple `impl Puddle for ExamplePuddle` should work, I'm noting that the fact it doesn't and should not and will never work means the different `impl

    Splash for ...` should definitely be resolvable, which is exactly like the AdditiveIterator example.

    – World Outsider Oct 13 '21 at 13:22