(Julia 1.0)以本地方式访问模块变量吗?

时间:2018-11-18 12:54:27

标签: julia

我有一个包含许多数学和物理常数的模块。就目前而言,我可以通过命名空间访问变量:

constants.jl

module Constant
export c

const c = 299792458  # m/s

end

working_example.jl

include("constants.jl")

println(Constant.c) # 299792458

但是,我希望将模块变量作为局部变量访问,例如E=m*c^2;

ideal.jl

include("constants.jl")

"""
something here
"""

println(c) # 299792458

我认为为了实现这一目标,我需要实现usingimport,但是在两种情况下我都会出错

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

将此内容添加到ideal.jl代码中不能解决问题:

not_working_example.jl

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

println(c)

由于错误而失败

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

我该如何解决这个问题?

P.S。我知道当前的命名约定并不理想-这并不是一成不变的,以后可能会更改。我正在使用Julia v1.0.2:

$ julia -v
julia version 1.0.2

1 个答案:

答案 0 :(得分:2)

您尝试过

include("constants.jl")
using .Constant

println(c)

请参见document

相关问题