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

Reloading functions, but not constants, from packages? #4008

Closed
timholy opened this issue Aug 10, 2013 · 5 comments
Closed

Reloading functions, but not constants, from packages? #4008

timholy opened this issue Aug 10, 2013 · 5 comments
Labels
speculative Whether the change will be implemented is speculative

Comments

@timholy
Copy link
Sponsor Member

timholy commented Aug 10, 2013

When developing modules/packages, reload() is often helpful for testing changes without having to restart the REPL. However, one issue is that

myvar = MyModule.Constructor(args...)
# make changes
reload("MyModule")
MyModule.runtest(myvar)

fails, because you have to re-create myvar. If you have variables that interact across packages, or take a long time to initialize (e.g., require nontrivial calculation), this can be a bit of a pain.

With a bit of social engineering, it seems like it should be possible to support something like reload("MyModule", constants=false). The idea is that packages wanting to support this functionality would be organized something like this:

MyModule/
    src/
        MyModule.jl           # loads everything
        constants.jl           # all type definitions and constants go here
        MyModuleCode.jl   # loads everything except constants.jl
        file1.jl
        file2.jl
        ...

From this gist, here's a demo session:

julia> import MyModule

julia> val = MyModule.MyType(7)
MyType(7)

julia> MyModule.bigger(val)
MyType(8)

julia> reload("MyModule")
Warning: replacing module MyModule

julia> MyModule.bigger(val)
ERROR: no method bigger(MyType,)

julia> val = MyModule.MyType(7)
MyType(7)

julia> MyModule.bigger(val)
MyType(8)

julia> eval(MyModule, :(include("MyModuleCode.jl")))
# methods for generic function bigger
bigger(x::MyType) at /tmp/MyModule/src/file1.jl:3

julia> MyModule.bigger(val)
MyType(8)
@quinnj
Copy link
Member

quinnj commented Aug 8, 2014

Seems like the functionality here is achieved through the new workspace() method; unless we still want to tweak reload?

@quinnj
Copy link
Member

quinnj commented Aug 21, 2014

Please reopen if tweaking reload is desired.

@quinnj quinnj closed this as completed Aug 21, 2014
@timholy
Copy link
Sponsor Member Author

timholy commented Aug 21, 2014

Perhaps even more relevantly, Julia now has much better smarts about detecting that types haven't changed. Try including a test file like

immutable MyType
    a::Int
end

f(::MyType) = 1

and then tweak the definition of f. You'll succeed, whereas formerly it would have complained about redefinition of MyType. In contrast, if you change MyType you'll still get an error (as well you should).

So yes, this can indeed be closed. Thanks for doing the cleaning!

@LMescheder
Copy link

LMescheder commented Sep 19, 2016

However, currently in julia v0.5-rc4 if I put

module A
immutable MyType
    a::Int
end

f(::MyType) = 1
end

in a file and want to change A.f, I have to reinitialize all variables of type A.MyType, which can be very inconvenient... Is there a workaround for this?

@cstjean
Copy link
Contributor

cstjean commented Sep 20, 2016

@LMescheder I hit the same problem with 0.5, and just wrote ClobberingReload.jl to solve it. It hasn't been tested much yet, but if you want to give it a try, it should solve your problem. Let me know if you have any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

4 participants