I would like to have the following functionality
trait Policy {
fn eval(&self, k: u32) -> bool;
fn default() -> Box<dyn Policy>
where
Self: Sized,
{
Box::new(MaxPolicy { max: 2 })
}
}
struct MaxPolicy {
max: u32,
}
impl Policy for MaxPolicy {
fn eval(&self, k: u32) -> bool {
println!("MaxPolicy");
k < self.max
}
}
#[test]
fn max_policy() {
let p = MaxPolicy { max: 2 };
assert!(!p.eval(3));
}
#[test]
fn default_policy() {
let p = Policy::default();
assert!(!p.eval(3));
}
This does not compile:
error[E0283]: type annotations needed
--> src/lib.rs:31:13
|
4 | fn default() -> Box<dyn Policy>
| -------
5 | where
6 | Self: Sized,
| ----- required by this bound in `Policy::default`
...
31 | let p = Policy::default();
| ^^^^^^^^^^^^^^^ cannot infer type
|
= note: cannot resolve `_: Policy`
Would it possible to alter the approach to make it work? Is this even possible for trait objects to have a method returning some implementation of Self
? If not, why not?