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

后端 未结 5 1376
青春惊慌失措
青春惊慌失措 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 16:50

    In my humble opinion, the better way is to use import from the very beginning instead of using for the reported issue.

    Consider the module:

    module ModuleX1
      export produce_text
      produce_text() = begin
        println("v1.0") 
      end
      println("v1.0 loaded")
    end
    

    Then in REPL:

    julia> import ModuleX1
    v1.0 loaded
    
    julia> ModuleX1.produce_text()
    v1.0
    

    Update the code of the module and save it:

    module ModuleX1
      export produce_text
      produce_text() = begin
        println("v2.0")  
      end
      println("v2.0 loaded")
    end
    

    Next, in the REPL:

    julia> reload("ModuleX1")
    Warning: replacing module ModuleX1
    v2.0 loaded
    
    julia> ModuleX1.produce_text()
    v2.0
    

    Advantages of using import over using:

    • avoiding ambiguity in function calls (What to call: ModuleX1.produce_text() or produce_text() after reloading?)
    • do not have to call workspace() in order to get rid of ambiguity

    Disadvantages of using import over using:

    • a fully qualified name in every call for every exported name is needed

    Edited: Discarded "full access to the module, even to the not-exported names" from "Disadvantages..." according to the conversation below.

提交回复
热议问题