Reload a module from within a while loop

question

#1

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?


#2

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


#3

See also: https://github.com/cstjean/ClobberingReload.jl


#4

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.


#5

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.


#6

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


#7

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