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

Using include inside __init__ causes using to not work right #26788

Closed
oxinabox opened this issue Apr 12, 2018 · 2 comments
Closed

Using include inside __init__ causes using to not work right #26788

oxinabox opened this issue Apr 12, 2018 · 2 comments

Comments

@oxinabox
Copy link
Contributor

oxinabox commented Apr 12, 2018

for Julia 0.6
Easier to explain with examples.
But basically if you put an include inside an __init__,
then it does not have access to names that were added to the calling namespace by using.

But if you put a using at the top of the included file, then all is good.
(But that I feel should be an error).

Works

all in one file: main.jl

module Foo 
    export fooit
    fooit()=println("IT was fooed")
end

module Bar
    using Foo
    __init__() = fooit()

end

using Bar

running that outputs "IT was fooed"

Doesn't work

Running main.jl gives
** InitError: LoadError: mUndefVarError: fooit not defined
while loading .../initstuff.jl **

in initstuff.jl:

fooit()

in main.jl:

module Foo
    export fooit
    fooit()=println("IT was fooed")
end


module Bar
    using Foo
    function __init__()
        include("initstuff.jl")
    end

end

using Bar

Works (but maybe shouldn't?)

I feel like this should be an error, as directly substituting the contents of initstuff.jl into __init__ would be an error.

in initstuff.jl:

using Foo
fooit()

in main.jl:

module Foo
    export fooit
    fooit()=println("IT was fooed")
end


module Bar
    using Foo
    function __init__()
        include("initstuff.jl")
    end

end

using Bar
@martinholters
Copy link
Member

Note that include does not substitute the contents at the place where it occurs, rather it evaluates them at the time the function is called. And AFAIU, in Julia 0.6 that happens to be Main unless the include happens while defining a module. Also, I don't think it's specific to __init__(). If you define another function in the same way and call it manually, you get the same error. On Julia master, every module gets its own include that evaluates into that module, so things work as expected there.

I'd like someone to verify, but I think the bottom line is that this is a known deficiency on 0.6 and fixed on master, so this issue could be closed.

@JeffBezanson
Copy link
Member

Yes, I believe that's correct.

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