避免每次修改函数时都重新启动 julia REPL

ima*_*tha 1 julia

我正在编写一个 julia 代码,其中有几个文件,并从这些文件调用函数到一个名为 的主函数run.jl。每次我对这些文件中的任何一个进行更改时,我都需要重新启动 Julia REPL,这有点烦人。无论如何要解决这个问题。例如

# --------------------------------- run.jl
include("agent_types.jl")
include("resc_funcs.jl")

using .AgentTypes: Cas, Resc
using .RescFuncs: update_rescuers_at_pma!, travel_to_loc

# ... some code

for i = 1:500
   update_rescuers_at_pma!(model)
   travel_to_loc(model)
end

# ------------------------------ resc_funcs.jl
function travel_to_loc(model)
    println(get_resc_by_prop(model, :on_way_to_iz))
    for resc_id in get_resc_by_prop(model,:on_way_to_iz)
        push!(model[resc_id].dist_traject, model[resc_id].dist_to_agent) #This is just for checking purposes
        model[resc_id].dist_to_agent -= model.dist_per_step 
        push!(model[resc_id].loc_traject, "on_way_to_iz")
    end
    #If we add new print statement here and execute run.jl it wont print
    println(model[resc_id].loc_traject) #new amendment to code
end
Run Code Online (Sandbox Code Playgroud)

但现在当我去更新travel_to_loc功能时。在反映这些更改之前,我需要重新启动 Julia repl。我想知道在保存文件(在本例中为 resc_funcs.jl)后是否有办法在执行run.jl.

Prz*_*fel 5

最简单的工作流程可能如下:

  1. 选择某个文件夹作为 Julia 中当前的工作文件夹(例如,通过使用cd()Julia 的命令)
  2. 创建示例文件MyMod.jl
    module MyMod
    
    export f1
    function f1(x)
        x+1
    end
    end
    
    Run Code Online (Sandbox Code Playgroud)
  3. 添加当前文件夹LOAD_PATH并加载Revise.jl
    push!(LOAD_PATH, ".")
    using Revise
    
    Run Code Online (Sandbox Code Playgroud)
  4. 加载模块并测试它:
    julia> using MyMod
    [ Info: Precompiling MyMod [top-level]
    
    julia> f1(3)
    4
    
    Run Code Online (Sandbox Code Playgroud)
  5. 尝试编辑文件,例如。改成x+1x+3
  6. 再次运行
    julia> f1(3)
    6
    
    Run Code Online (Sandbox Code Playgroud)

笔记:

  • struct当数据结构发生变化时(修改对象的定义),您仍然需要重新启动 REPL
  • 您可以使用生成完整的模块包Pkg.generate,但我想让事情变得简单。