On 7/13/12 12:38 PM, David Bruant wrote:
I can't help but asking why can't named function close over their
environment?
In the case above, I see no harm in allowing the 'bar' function to
access 'foo'.
Because named functions are always in scope (they're always mutually
recursive), while locals are not. For example, these two are equivalent:
fn f() {
g();
fn g() { ... }
}
fn f() {
fn g() { ... }
g();
}
But if we allowed items to close over variables, then that wouldn't be
the case anymore. This cannot be allowed:
fn f() {
g();
let x = ...;
fn g() { ... use x ... }
}
Because x has not yet been initialized.
Yet the mutual recursion property is very useful; consider:
fn f() {
fn g() { ... h(); ... }
fn h() { ... g(); ... }
}
If functions were not mutually recursive, you could not write this.
Patrick
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev