Skip to content

Julia for-loop variable scoping #25724

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

Closed
andreas-ernst opened this issue Jan 24, 2018 · 1 comment
Closed

Julia for-loop variable scoping #25724

andreas-ernst opened this issue Jan 24, 2018 · 1 comment
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@andreas-ernst
Copy link

Compare the following nearly identical bits of code:

for i=1:3
    needX = false
    try
        X[1] = X[1] + 1
    catch err
        print(err)     
        needX = true
    end
    if needX
        X = [0]
    end
    println("  X=",X)
end

Output:

UndefVarError(:X)  X=[0]
UndefVarError(:X)  X=[0]
UndefVarError(:X)  X=[0]

Now a statement that should do nothing (X=X) to get:

for i=1:3
    needX = false
    try
        X = X # without this statement we create an UndefVarError every time
        X[1] = X[1] + 1
    catch err
        print(err)     
        needX = true
    end
    if needX
        X = [0]
    end
    println("  X=",X)
end

to get the output

UndefVarError(:X)  X=[0]
  X=[1]
  X=[2]

Another interesting variant on this theme:

for i=1:3
    needX,needY = false,false
    try
       x[1] = x[1] + 1
    catch err
        print(err)
        needX = true
    end
    if needX
        x = [0]
    end
    try
        y = y + 1
    catch err
        print(err)     
        needY = true
    end
    if needY
        y = 0
    end
    println("  x=",x,"  y=",y)
end

for i=1:3
    needY = false
    try
        Y = Y + 1
    catch err
        print(err)     
        needY = true
    end
    if needY
        Y = 0
    end
    println("  Y=",Y)
end

output

UndefVarError(:x)UndefVarError(:y)  x=[0]  y=0
UndefVarError(:x)UndefVarError(:y)  x=[0]  y=0
UndefVarError(:x)UndefVarError(:y)  x=[0]  y=0
UndefVarError(:Y)  Y=0
  Y=1
  Y=2

Adding the x=x or y=y statements retains the x variables in the first loop but not the y variables.

Perhaps an indication that the soft scoping rules of for-loops are a bit too baroque.

Version used for testing:

Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)
@JeffBezanson
Copy link
Member

Perhaps an indication that the soft scoping rules of for-loops are a bit too baroque.

No, I suspect it's a bug. "Soft scope" (which no longer exists in v0.7) only has to do with writing to global variables.

@JeffBezanson JeffBezanson self-assigned this Jan 24, 2018
@JeffBezanson JeffBezanson added bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage) labels Jan 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

2 participants