Force update const in module

I have created a module that read some data from files. I tried storing the file content in a variable like shown below. But this method doesn’t update the name variable from the file even if i restart the julia session. Is there any way to force reading the file and redefine the constant when I import the package?

module TestModule

const name = open(expanduser("~/name")) do io
    String(read(io))
end

greet() = print("Hello $name")

end # module TestModule

Add

include_dependency(expanduser("~/name"))

to the module definition. Then any time you change that file, the package will be recompiled.

But if you just want it to be a runtime constant, then use

const name = Ref{String}()

function __init__()
    name[] = open(expanduser("~/name")) do io
        String(read(io))
    end
end

and then use name[] everywhere you’re using name now. Then it will be reloaded in every fresh Julia session.

2 Likes