Importing constant definition from a module file


#1

I would like to create a module file “defs.jl” that contains definitions of several constants such a grid size, dimensionality etc. To do so I have created the following module file:

defs.jl

module defs
export D
const D=2 # dimension
end

and import it in my code using the command “include(”./defs.jl")".

My problem is that in order to get the value of D I have to type “defs.D” instead of directly D even though D is exported. I tried using “using defs” but that results in an error “Module defs not found in current path.”

Thanks!


#2

When importing a submodule, and not just a file with code, into your overall module, it is necessary to do it this way (and the capitalization is good practice):

MyDefs.jl

module MyDefs

export NDIMS

const NDIMS = 2

end # module

and in the same directory
MyModule.jl

module MyModule

export test

include("MyDefs.jl")

using .MyDefs  # note the `.` before the local module name

function test()
    println("NDIMS = $(NDIMS)")
end

end # module

then

using MyModule

test() 
# NDIMS = 2