Implementing function composition

The problem is that H is an input parameter. It'd be the same as if you said this:

fn show<T: Display>() -> T {
   "Hello, world"
}

This type signature is wrong, because it says I could call show::<u32> and get a u32, when show simply returns a string every time.

The way to solve this problem on stable right now is to use a trait object:

fn show() -> Box<Display> {
    Box::new("Hello, world")
}

fn comp<F, Fin, Fout, G, Gout>(g: G, f: F) -> Box<Fn(Fin) -> Gout>
where F: Fn(Fin) -> Fout, G: Fn(Fout) -> Gout {
    Box::new(|x| g(f(x))
}
1 Like