Hacker News new | past | comments | ask | show | jobs | submit login

Or only allow shadowing for `var` and forbid it for `:=`[0]. Though forbidding it entirely would work just as well.

[0] and go is actually weirder than that — and the opposite way 'round — as `var` doesn't allow any shadowing in the same scope:

    var a, b int
    var a, c int // fails because it redeclares a in the same block
while `:=` allows same-scope shadowing as long as the overlap is not complete:

    a, b := foo()
    a, b := foo() // fails because no new variable on the left side
    a, c := foo() // succeeds
both allow arbitrary shadowing in sub-scopes.



There's no shadowing in the latter case. The second case is the same thing as

    a := 2
    a = 3
By definition you must have nested scopes to have shadowing. Within the same scope, it's only ever assignment.


Well, in Rust you could do:

    let a = 2;
    let a = 3;
I think you would say the latter shadows the former...


Indeed, because semantically there is a syntactically implicit scope for every let binding. For example, in that case, the outer a is dropped after the inner a, just as if the second a had been inside of a block. There may be multiple syntactic ways to introduce a new scope.


That last one isn’t shadowing. := reuses a variable of the same name in the same scope.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: