After some experiment and searching, I figured out 2 ways of creating a shared module that holds some constant values.
SCHEME A:
# in file sharedconstants.jl:
module sharedconstants
kelvin = 273.15
end
# -------------------------
# in file main.jl:
include("./sharedconstants.jl");
using .sharedconstants
print(sharedconstants.kelvin, "\n");
# -------------------------
SCHEME B:
# in file sharedconstants.jl:
module sharedconstants
kelvin = 273.15
end
# -------------------------
# in file main.jl:
import sharedconstants
print(sharedconstants.kelvin, "\n");
# -------------------------
Scheme B does not always work and when it fails it throws
the error of not finding sharedconstants in current Path. Plus, Scheme B
requires the name of module (sharedconstants) the same as the trunk of
the file name. I wonder which way of the above is better in terms of
compiling and execution. Also is there any other approach to do the job?
I transferred from FORTRAN and I am quite used to simply
use sharedconstants
in my code.
For performance reasons this should be a const
(BTW module names use CamelNaming):
module SharedConstants2
const kelvin = 273.15
end
Writing it this way makes it type-stable which results in huge performance difference:
julia> @btime sharedconstants.kelvin * 3
18.574 ns (1 allocation: 16 bytes)
819.4499999999999
julia> @btime SharedConstants2.kelvin * 3
0.001 ns (0 allocations: 0 bytes)
819.4499999999999
Regarding the question "where to place it" I would recommend doing a Julia package - start reading here: https://pkgdocs.julialang.org/v1/creating-packages/
Finally, you might have a look at the PhysicalConstants.jl
package https://github.com/JuliaPhysics/PhysicalConstants.jl