Thanks for the answers,
Yeah, as I thought.
Currently, it is impossible to create a closure that returns other closure without allocating memory in heap.
Why I think this is possible because returning closure doesn’t need allocating memory in heap, closure returning another closure is still a closure.
Conside following code
/*
in haskell:
get_fun2 = \() -> \() -> \() -> 42
do_it f = print(f()())
a = get_fun2()
do_it(a)
*/
struct Closure1(usize);
impl Closure1 {
fn call(&self) -> usize {
self.0
}
}
struct Closure2(Closure1);
impl Closure2 {
fn call(&self) -> &Closure1 {
return &self.0
}
}
fn get_fun2() -> Closure2 {
Closure2(Closure1(42))
}
fn do_it(c: Closure2) {
println!("{}", c.call().call())
}
fn main() {
let a = get_fun2();
do_it(a);
}
in that code, we pretend Closure1 and Closure2 are generated by the compiler, as you can see, it is possible to create a closure that returning another closure without needing for allocating memory in heap.
It is just, it is not possible to express it as impl Trait