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

occasional error for "using Foo" inside if statement #6901

Closed
stevengj opened this issue May 20, 2014 · 4 comments
Closed

occasional error for "using Foo" inside if statement #6901

stevengj opened this issue May 20, 2014 · 4 comments

Comments

@stevengj
Copy link
Member

Given

module Foo
const foo = 3
end

I get error compiling anonymous: unsupported or misplaced expression using in function anonymous for

if true
    using Foo
    for m in 1:3
        println(m)
    end
end

but on the other hand the following works fine:

if true
    using Foo
    println("ok")
end

I'm don't understand why using would be allowed in one if statement but not in the other.

@JeffBezanson
Copy link
Member

Probably same issue as #2586

Due to the introduction of variables with assignments, our scopes can be specified "inside out" --- whether something is a scope can depend on what is inside. This can prevent a block from being at the top level, thereby forbidding top-level-only expressions.

@stevengj
Copy link
Member Author

I thought if statements never introduce a new scope?

@JeffBezanson
Copy link
Member

The if statement doesn't really (adding x=1 above the for loop will indeed assign x globally). It's just that the compiler is being lazy and lumping the whole expression into one "non-top-level-thing" even though only the for loop part of it actually creates a scope.

@JeffBezanson
Copy link
Member

Closing as dup

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

2 participants