DocDave
DocDave

Reputation: 146

(Julia 1.0) Accessing Module Variables as Local?

I have a module containing a number of mathematical and physical constants. As it stands, I am able to access the variables through namespacing:

constants.jl

module Constant
export c

const c = 299792458  # m/s

end

working_example.jl

include("constants.jl")

println(Constant.c) # 299792458

However, I am looking to access the module variables as local variables e.g. E=m*c^2;

ideal.jl

include("constants.jl")

"""
something here
"""

println(c) # 299792458

I assume that in order to achieve this I need to implement either using or import, however in both scenarios I get error

ERROR: LoadError: ArgumentError: Package Constant not found in current path:
- Run `import Pkg; Pkg.add("Constant")` to install the Constant package.

Adding this to the ideal.jl code does not solve the problem:

not_working_example.jl

include("constants.jl")
import Pkg
Pkg.add("Constant")
import Constant

println(c)

which fails with error

ERROR: LoadError: The following package names could not be resolved:
 * Constant (not found in project, manifest or registry)
Please specify by known `name=uuid`.

How do I go about resolving this?

P.S. I am aware that the current naming convention is not ideal - this is not set in stone and may change at a later date. I am using Julia v1.0.2:

$ julia -v
julia version 1.0.2

Upvotes: 2

Views: 243

Answers (1)

张实唯
张实唯

Reputation: 2862

have you tried

include("constants.jl")
using .Constant

println(c)

see the document

Upvotes: 3

Related Questions