6

I am trying to build a solution to Graham´s accumulator factory challenge which basically requires a function to return a closure that closes over a mutable numeric variable which initial value is received through a parameter. Each call to this closure increments this captured variable by a value that is a parameter to the closure and returns the accumulated value.

After reading the closures RFC and some questions about returning unboxed closures (in particular this). I could finally come up with a solution that compiled, but the result is not what I would expect.

#![feature(unboxed_closures, unboxed_closure_sugar)]

fn accumulator_factory(n: f64) -> Box<|&mut: f64| -> f64> {
    let mut acc = n;
    box |&mut: i: f64| {
        acc += i; 
        acc
    }
}

fn main() {
    let mut acc_cl = accumulator_factory(5f64);
    println!("{}", acc_cl.call_mut((3f64,)));
    println!("{}", acc_cl.call_mut((3f64,)));
}

AFAIK this closure captures acc by value, the generated struct that acts as the environment is mutable and acc_cl should retain the same environment instance between calls.

But the printed result is 6 in both cases, so it seems the modified value is not persisting. And what is more confusing is how this result is computed. On each execution of the closure the initial value of acc is 3 even though n is 5 when called.

If I modify the generator to this:

fn accumulator_factory(n: f64) -> Box<|&mut: f64| -> f64> {
    println!("n {}", n);
    let mut acc = n;
    box |&mut: i: f64| {
        acc += i; 
        acc
    }
}

then the execution always return 3 and the initial value of acc is always 0 on closure entry.

This difference in semantics looks like a bug. But apart from that, why is the environment reset between calls?

This was run with rustc 0.12.0.

8

The capture mode of closures has changed recently. Closures are biased to capture everything by reference because the most common use case for closures is passing them to functions down the call stack, and capturing by reference makes working with the environment more natural.

Sometimes capture by reference is limiting. For example, you can't return such closures from functions because their environment is tied to the call stack. For such closures you need to put the move keyword before the closure:

fn accumulator_factory(n: f64) -> Box<FnMut(f64) -> f64> {
    println!("n: {}", n);
    let mut acc = n;
    Box::new(move |i: f64| {
        acc += i; 
        acc
    })
}

fn main() {
    let mut acc = accumulator_factory(10.0);
    println!("{}", acc(12.0));
    println!("{}", acc(12.0));
}

This program works as intended:

n: 10
22
34

These two closure kinds are covered by this RFC.

  • Could you call acc(12.0) instead of acc.call_mut((12.0,)) ? The call syntax seems a bit overwhelming. – Matthieu M. Oct 22 '14 at 17:50
  • @MatthieuM., that's why I said that I don't understand why overloaded calls don't work. They are also feature-gated, but even with this gate lifted the compiler still complains. – Vladimir Matveev Oct 22 '14 at 17:52
  • @VladimirMatveev Thanks. I wasn´t aware of this change in syntax. I guess things are still changing very fast. Now when I try to change f64 to a generic type I´m running into lifetime issues. But I will write another question for that. – Juan Vidal Oct 22 '14 at 19:21
  • I edited the code above to make it work on Rust 1.9 nightly. Unboxed closures has been implemented and the syntax was changed as per: github.com/rust-lang/rfcs/blob/master/text/… – CMCDragonkai Mar 29 '16 at 13:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.