Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested function introduces a variable scoping bug #11065

Closed
Sisyphuss opened this issue Apr 30, 2015 · 5 comments
Closed

Nested function introduces a variable scoping bug #11065

Sisyphuss opened this issue Apr 30, 2015 · 5 comments
Assignees

Comments

@Sisyphuss
Copy link

This is a bug related to the discussion in [https://groups.google.com/forum/?fromgroups=#!topic/julia-users/ddsrBhXFJsc]:

I modified the code and tested it (v0.3.6):
Normal code:

for i = 1:2
    if i == 1
        z = "z is defined" 
        println("Iteration $i: $z") 
    elseif i == 2
        try
            println("Iteration $i: $z")
        catch
            println("Iteration $i: z is undefined")
        end 
    end
end 

Iteration 1: z is defined
Iteration 2: z is defined

Now I define a function g() inside and get a bug:

for i = 1:2
    g() = z
    if i == 1
        z = "z is defined"  
        println("Iteration $i: $z") 
    elseif i == 2
        try
            println("Iteration $i: $z")
        catch
            println("Iteration $i: z is undefined")
        end 
    end
end 

Iteration 1: z is defined
Iteration 2: z is undefined

[pao: syntax highlighting]

@JeffBezanson
Copy link
Member

Arguably the second behavior is the correct one, and the first one is the bug. Variables only used within a loop are supposed to be newly allocated on each iteration.

@vtjnash vtjnash changed the title Nested function introduces a bug Nested function introduces a variable scoping bug May 1, 2015
@Sisyphuss
Copy link
Author

Variables only used within a loop are supposed to be newly allocated on each iteration.

This remark is inaccurate. I rewrite the above examples with while loop:

i = 1
while i <= 2
    if i == 1
        z = "z is defined" 
        println("Iteration $i: $z") 
    elseif i == 2
        try
            println("Iteration $i: $z")
        catch
            println("Iteration $i: z is undefined")
        end 
    end
    i += 1
end 

Iteration 1: z is defined
Iteration 2: z is defined

i = 1
while i <= 2
    g() = z
    if i == 1
        z = "z is defined" 
        println("Iteration $i: $z") 
    elseif i == 2
        try
            println("Iteration $i: $z")
        catch
            println("Iteration $i: z is undefined")
        end 
    end
    i += 1
end 

Iteration 1: z is defined
Iteration 2: z is defined

Now in the second example, z is still defined.

@JeffBezanson
Copy link
Member

Turns out this issue was described in this comment: #1571 (comment)

@JeffBezanson
Copy link
Member

Also, I should have said "for loop" instead of "loop". for loops create a new scope on each iteration; while loops do not. The idea is that while is more of an imperative construct, and for is "parallel".

@JeffBezanson JeffBezanson self-assigned this May 29, 2015
@johnmyleswhite
Copy link
Member

Are there performance implications to having while create a new scope on each iteration? It seems to me that making while and for as similar as possible would be desirable, all else being equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants