避免每次对函数进行修改时重新启动julia REPL



我正在编写一个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

但现在,当我去更新travel_to_loc函数时,例如。我需要重新启动julia repl,然后这些更改才会反映出来。我想知道在保存文件(在本例中为resc_funcs.jl(后,是否有办法在执行run.jl时反映这些修改。

最简单的工作流程如下:

  1. 选择一些文件夹作为您在Julia中的当前工作文件夹(例如,使用Julia的cd()命令(
  2. 创建示例文件MyMod.jl
    module MyMod
    export f1
    function f1(x)
    x+1
    end
    end
    
  3. 将当前文件夹添加到LOAD_PATH并加载Revise.jl
    push!(LOAD_PATH, ".")
    using Revise
    
  4. 加载模块并进行测试:
    julia> using MyMod
    [ Info: Precompiling MyMod [top-level]
    julia> f1(3)
    4
    
  5. 尝试编辑文件,例如将x+1更改为x+3
  6. 再次运行
    julia> f1(3)
    6
    

备注:

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

相关内容

最新更新