I would like to make a struct holding a closure.
Also there should be a way to make kind 'default' value of the struct type which is holding no or void computation.
Here is my attempt;
struct Computation<X, Y, F>
where
F: Fn(&X) -> Y,
{
f: F,
_p: PhantomData<(X, Y)>,
}
impl<X, Y, F> Computation<X, Y, F>
where
F: Fn(&X) -> Y,
{
fn new(f: F) -> Self {
Computation {
f: f,
_p: PhantomData,
}
}
fn default() -> Self {
Computation::new(|&_|()) // <- ERROR
}
}
And here is the compiler output.
mismatched types
expected type parameter `F`
found closure `[closure@src/lib.rs:212:26: 212:30]`
every closure has a distinct type and so could not always match the caller-chosen type of parameter `F`rustcClick for full compiler diagnostic
lib.rs(200, 12): this type parameter
lib.rs(212, 9): arguments to this function are incorrect
lib.rs(204, 8): associated function defined here
It seems that trying to specifying the concrete type of F 'after the call` if default is the problem. Then, is there any way to implement 'default constructor' for these kind of structs?