imantha
imantha

Reputation: 3828

Avoid restarting julia REPL everytime you make an ammendment to a function

I am working on a julia code where I have several files and call functions from these files to a main function called run.jl. Every time I make changes to any one of these files I need to restart the julia REPL which is a bit annoying. Anyway to work around this. For example

# --------------------------------- 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

But now when I go and update travel_to_loc function for example. I need to restart the julia repl before those changes are reflected. I am wondering if there is a way after you save the file (resc_funcs.jl in this case) those amendments are reflected when you execute run.jl.

Upvotes: 1

Views: 907

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

The simplest workflow could be the following:

  1. Select some folder to be your current working folder in Julia (eg. by using cd() command from Julia)
  2. Create sample file MyMod.jl
    module MyMod
    
    export f1
    function f1(x)
        x+1
    end
    end
    
  3. Add the current folder to LOAD_PATH and load Revise.jl
    push!(LOAD_PATH, ".")
    using Revise
    
  4. Load the module and test it:
    julia> using MyMod
    [ Info: Precompiling MyMod [top-level]
    
    julia> f1(3)
    4
    
  5. Try to edit the file eg. change x+1 to x+3
  6. Run again
    julia> f1(3)
    6
    

Notes:

  • you will still need to restart REPL when your data structures change (you modify a definition of a struct object)
  • you could generate a full module package using Pkg.generate but I wanted to make things simplified.

Upvotes: 5

Related Questions