How to specify types on this generic partial application?
ā Rust š 2025-07-30 š¤ surdeus šļø 12Is it possible to create make_adapter here? playground link
The basic idea is simple - Iām binding an argument, so I can then create multiple partial applications by calling the result of make_adapter. But I can't figure out how to specify the types.
fn make_adapter<F1, F2>(b: i32) -> impl Fn(F2) -> F1
where F1: Fn(i32) -> (), F2: Fn(i32, i32) -> ()
{
let adapt = |f2: F2| -> F1 {
let f1 = move |a: i32| {
f2(a, b);
};
f1
};
adapt
}
fn main() {
let a: i32 = 123;
let adapt = make_adapter(456);
let f1: Box<dyn Fn(i32) -> ()> = Box::new(
adapt(|a,b| println!("called: {}, {}", a, b))
);
f1(a);
}
Errors:
error[E0308]: mismatched types
--> src/main.rs:9:9
|
2 | fn make_adapter<F1, F2>(b: i32) -> impl Fn(F2) -> F1
| -- expected this type parameter
...
5 | let adapt = |f2: F2| -> F1 {
| -- expected `F1` because of return type
6 | let f1 = move |a: i32| {
| ------------- the found closure
...
9 | f1
| ^^ expected type parameter `F1`, found closure
|
= note: expected type parameter `F1`
found closure `{closure@src/main.rs:6:18: 6:31}`
= help: every closure has a distinct type and so could not always match the caller-chosen type of parameter `F1`
error[E0283]: type annotations needed
--> src/main.rs:17:38
|
16 | let adapt = make_adapter(456);
| ----------------- type must be known at this point
17 | let f1: Box<dyn Fn(i32) -> ()> = Box::new(
| ^^^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Box`
|
3 posts - 2 participants
š·ļø Rust_feed