I am trying to implement a certain probability distribution. This distribution has an exact and an approximate form. Generally this goes well:
struct MyDistribution {
approximation: Approximation,
}
enum Approximation {
Existing(ExistingDistribution),
Exact,
}
impl Continuous for MyDistribution {
fn pdf(&self, x: f64) -> f64 {
match self.approximation {
Approximation::Existing(distribution) => distribution.pdf(x),
Approximation::Exact => {
// Some calculations
todo!();
}
}
}
}
However, in one of the trait implementations I want to use the default implementation for Approximation::Exact
and a specific implementation from ExistingApproximation
for Approximation::Existing
. I don't think that is possible (implementing traits for variants) even though it should be fine if all the variants have an implementation. I could make a separate struct for the exact distribution and then wrap both in MyDistribution
but that would duplicate a lot of code. Is there a better approach?