How do I reload a module under development in Julia 1.6?

Viewed 184

I know this question has been asked and answered before, but none of the many answers work for me as described.

What is the procedure for reloading a module that I'm working on in Julia (1.6)?

For example, I have

module MyModule

export letters

const letters = String('A':'Z')

end

and I want the be able to load the module, make changes to letters in the module's file, and then reload the module and have those changes reflected in subsequent uses of letters. This seems simple enough, but I can't get it to work.

I've tried

include("src/MyModule.jl")
using .MyModule

but if I change the definition of letters in MyModule.jl and then

include("src/MyModule.jl")

letters doesn't change, unless I fully qualify its use each time with Main.MyModule.letters: using Main.MyModule; letters refers, for example, to the old definition.

How do I reload a module under development so that I can refer to its definitions without fully qualifying them (and without having an unqualified shadow definition always lying around)?

1 Answers

I would just use Revise.jl and wrap everything in functions:

module MyModule

export letters

letters(char_start, char_end) = char_start:char_end |> String

end
julia> using Revise

julia> includet("src/MyModule.jl")

julia> using .MyModule

julia> letters('l', 'p')
"lmnop"
module MyModule

export letters

letters(char_start, char_end) = char_start:char_start |> String

end
julia> letters('l', 'p')
"l"

const is for defining things that you do not want to modify, so I would not expect your original version to work as expected. Revise.jl should also throw a redefinition error if you try to change it

In general though, it's usually much nicer (and easier too!) to just put everything in a package and use the usual using/import syntax. PkgTemplates.jl is great for this

If you would like to redefine consts though, I would definitely recommend checking out Pluto.jl

Related