Hi Everybody,
I have a function that takes an optional argument with a... shall I say... specific, type.
struct MyStruct{}
impl MyStruct {
pub fn do_something <
ClosureResultT: PartialOrd + Copy + Default + From<f64>,
MyClosureWithLotsOfArgs: Fn(
usize, usize,
&[i16; 4]) -> Option<ClosureResultT>,
>(&self, my_closure : &Option<MyClosureWithLotsOfArgs>) {
}
}
fn main() {
let my_struct = MyStruct{};
my_struct.do_something(&None);
}
But 90% of callers want to just call do_something(&None)
.
So is there a way around having to specify something like
let my_none : Option<Fn(usize, usize, &[i16; 4]) -> Option<f64>> = None;
do_something(&my_none);
Not to mention that doesn't compile either... To actually get it to compile, I need to define the closure type in the calling function's (or impl's) type definitions.
Anyway. This is pretty hideous, and I was wondering if anyone else has come up with an elegant solution for this kind of situation.
I should also mention, that I understand the compiler needs to know the type of the parameter to generate code for the side of the branch when the Option isn't None, but I'd be happy with a solution that compile-time specialized the function into a version that totally omitted the non-None branches. But I couldn't figure out the right way to do that without some really ugly macros.
Thanks in advance.