Quick query about modules.
Is there any difference between writing a module with initialized global variables and using an __init__
function to initialize those variables?
Allow me to explain with two demonstrations modules:
# ModuleA.jl
module ModuleA
global gCache = Dict()
end
# ModuleB.jl
module ModuleB
function __init__()
global gCache
gCache = Dict()
end
end
I’m curious as to whether there is any material difference between these two codes.
In addition, there is an auxillary point related to VS Code and the Julia Extension.
- In the case of
ModuleA
, the Julia Extension recognizes the variablegCache
- In the case of
ModuleB
, the Julia Extension discorages writing code in this way as it marks the global variablegCache
as an undefined reference
I wonder if anyone can comment on this. Is this simply a bug in the Julia Extension? Or is something more significant going on here?