Reload a module from within a while loop

I wish to reload a module based upon some user input.

The following works…

while true
    import mod
    readline()
    reload("mod")
    mod.test()
end

but this fails to reload the module…

import mod

while true
    readline()
    reload("mod")
    mod.test()
end

I assume due to some scoping rules. How can one achieve the same functionality without importing inside the while loop?

import and reload (or rather require which does the real work) involve convoluted logic. Unless you need some feature of packages, you could do this (without the import):

while true
    readline()
    include("mod.jl")
    mod.test()
end

which exploits the fact that include operates at global scope (so module definitions are ok).

See also: GitHub - cstjean/ClobberingReload.jl: reload for interactive work + Autoreload successor

ClobberingReload.creload might work, but the fundamental issue is likely that mod.test gets hard-linked by the compiler, so maybe you should try:

while true
    readline()
    reload("mod")   # or creload
    @eval mod.test()
end

It will force the compiler to recompile the mod.test call at every iteration of the loop.

I do use ClobberingReload, but on this occasion I had some difficulties (@__FILE__ statement returning nothing when evaluated during reloading). I will open an issue if I can’t figure it out.

Please do. The @__FILE__ problem might be this issue, which is potentially solved on master. I’ll tag it next week.

Yes, this is fixed on master. Thank you for your help.