3

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)?

orome
  • 45,163
  • 57
  • 202
  • 418
  • Related discussion: [How do I reload a module in an active Julia session after an edit?](https://stackoverflow.com/questions/25028873/how-do-i-reload-a-module-in-an-active-julia-session-after-an-edit) – Ian Weaver Jul 09 '21 at 19:46
  • @IanWeaver Yes and no. It answers it in several different ways, many of which are clearly outdated, and none of which works. – orome Jul 09 '21 at 19:48
  • 1
    Yea, having a bunch of old/conflicting answers can be frustrating. I was referring to the more recent answer in that thread that the OP added to their question: https://stackoverflow.com/a/50816280/16402912. I added more details below – Ian Weaver Jul 09 '21 at 19:51

1 Answers1

5

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

Ian Weaver
  • 150
  • 9