Hy, I wanted to write a library witch leverage some closure composition
but stumbled about some fundermental problems:
Cutting a complex Example Short:
fn compose<IN,OUT,OLD,NEW>(func: OLD) -> NEW
where OLD: Fn( IN ) -> OUT,
NEW: Fn( IN ) -> OUT
{
| data: IN | -> OUT {
//DO SOME STUF
func(data)
}
}
fn main() {
let nfunc = compose( | x: u32 | -> u32 {
x * 2
} );
let res = (nfunc)( 12u32 );
println!("{}", res);
}
This leads to 2 Errors:
-
I cannot return the closure because the
NEW
type does not match the closure type (but It should) -
The compiler wants a explicit type for
res
but this is the full type of the closure created incompose
so I can not write it down (but the compiler should be able to infer it based on the return type) this might be fixed if 1. is fixed
Does anyone has a Idea how to solve this (maybe bug). Note that in the
real code the closure closes over some data in a way witch makes it impossible
to return a function pointer or a struck containing some data and a function pointer.