Just for the sake of clarity I defined a portion of code as a closure, like that:
let mut wrap = |action| {
let () = action();
last_check_time = Instant::now();
};
wrap(|| {
...
});
I'm using the closure in just one place. In the ... argument I do normal stuff like call some functions on the captured variables.
The code portion doesn't compile with:
error[E0282]: type annotations needed
--> my_code/run.rs:34:30
|
34 | let mut wrap = |action| {
35 | let () = action();
| -------- type must be known at this point
but I believe there's no ambiguity left?
This is a problem to me because the type of the closure I pass as an argument is an opaque closure#295. I can't specify the type of wrap to be impl FnMut(impl Fn()) because that's disallowed in closure type annotations.
In general, closures can have lots of different signatures. There might be no distinctions worth making here, but the compiler isn't perfectly able to detect that and avoid considering this ambiguous.
What can I do
Introduce a function that pins down the function signature you want the closure to have.
fn closure_cast<F: Fn()>(f: F) -> F {
f
}
...
wrap(closure_cast(|| {
...
}));
Being able to call a function is based on trait implementations,[1] and something like this is ambiguous too.
let mut wrap = |usable| {
// Could be many implementors (or could even be an inherent method)
let () = usable.into();
last_check_time = Instant::now();
};
For example, every closure has a distinct type....