← Back to context

Comment by delian66

3 years ago

> I see though that V supports closures. What are the rules for shadowing variables in closures?

    fn main() {
        x := 1
        y := fn (x int) {
            println(x)
        }
        y(x)
        y(2)
    }

I am not sure I follow - the `x` parameter for the anonymous function, is entirely different, than the `x` in the main function. For me, there is no way for it to be confused with the `x` inside main.

... y := fn [x] (x int) { println(x) } ...

> Well, that seems like it should be disallowed. It makes sense that x can be captured but to then shadow the argument with the same name without error or warning doesn’t seem inline with the rest of V’s behavior.

I see what you mean now, yes, that does seem like another good issue candidate. Filed in https://news.ycombinator.com/item?id=31794565