On May 29, 2013, at 6:48 AM, Gábor Horváth wrote:
> Hi!
>
> The following code snippet is well formed and prints 6:
>
> fn main() {
> let x = 5;
> let x = 6;
> println(fmt!("%?",x));
> }
>
> I was told this behavior is intentional. However I consider this pattern as a
> potential source of errors.
> Consider a larger function which one wants to modify. One declares a new
> variable in the middle of the function, and if there was a name collosion the
> whole meaning of the function is changed and the compiler does not even
> provide us with a warning about it.
>
> I'm curious what was the reason of allowing multiple variables with the same
> name in the same scope?
I think this idiom is quite common. In at least one set of cases, it can help
*avoid* errors:
let x = compute_some_value();
// uh oh, clean up value of x:
let x = clean_up(x);
use_of(x);
use_of(x);
In this case, you may want to ensure that the earlier binding of 'x' is no
longer visible.
So, it's a tradeoff: disallowing re-use of names ensures that you can easily
spot the binding site; allowing re-use of names allows you to limit the
possible uses of the binding.
Naturally, I speak only for myself.
Best,
John Clements
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev