How do I reload a module in an active Julia session after an edit?

后端 未结 5 1345
青春惊慌失措
青春惊慌失措 2020-12-04 16:07

2018 Update: Be sure to check all the responses, as the answer to this question has changed multiple times over the years. At the time of this update, the <

5条回答
  •  -上瘾入骨i
    2020-12-04 16:55

    The basis of this problem is the confluence of reloading a module, but not being able to redefine a thing in the module Main (see the documentation here) -- that is at least until the new function workspace() was made available on July 13 2014. Recent versions of the 0.3 pre-release should have it.

    Before workspace()

    Consider the following simplistic module

    module TstMod
    export f
    
    function f()
       return 1
    end
    
    end
    

    Then use it....

    julia> using TstMod
    
    julia> f()
    1
    

    If the function f() is changed to return 2 and the module is reloaded, f is in fact updated. But not redefined in module Main.

    julia> reload("TstMod")
    Warning: replacing module TstMod
    
    julia> TstMod.f()
    2
    
    julia> f()
    1
    

    The following warnings make the problem clear

    julia> using TstMod
    Warning: using TstMod.f in module Main conflicts with an existing identifier.
    
    julia> using TstMod.f
    Warning: ignoring conflicting import of TstMod.f into Main
    

    Using workspace()

    However, the new function workspace() clears Main preparing it for reloading TstMod

    julia> workspace()
    
    julia> reload("TstMod")
    
    julia> using TstMod
    
    julia> f()
    2
    

    Also, the previous Main is stored as LastMain

    julia> whos()
    Base                          Module
    Core                          Module
    LastMain                      Module
    Main                          Module
    TstMod                        Module
    ans                           Nothing
    
    julia> LastMain.f()
    1
    

提交回复
热议问题