我想确认我不能有一个名为 Test
的结构体在定义为 Test
的模块中定义.
文件 main.jl :
# MAIN PROGRAM
include("test.jl")
import .Test
t = Test.Test(3,4)
println(t)
文件 test.jl :
module Test
struct Test
a::Int
b::Int
end
end
我得到:
$ /usr/local/julia-1.2.0/bin/julia main.jl
ERROR: LoadError: LoadError: invalid redefinition of constant Test
Stacktrace:
[1] top-level scope at /usr/home/user/test/test.jl:3
[2] include at ./boot.jl:328 [inlined]
[3] include_relative(::Module, ::String) at ./loading.jl:1094
[4] include(::Module, ::String) at ./Base.jl:31
[5] include(::String) at ./client.jl:431
[6] top-level scope at /usr/home/user/test/main.jl:3
[7] include at ./boot.jl:328 [inlined]
[8] include_relative(::Module, ::String) at ./loading.jl:1094
[9] include(::Module, ::String) at ./Base.jl:31
[10] exec_options(::Base.JLOptions) at ./client.jl:295
[11] _start() at ./client.jl:464
in expression starting at /usr/home/user/test/test.jl:3
in expression starting at /usr/home/user/test/main.jl:3
最佳答案
I would like to confirm that I can not have a struct named Test defined in a module defined as Test.
你不能。每个模块都有一个自绑定(bind),所以在
Test
里面模块Test
name 已经被占用(由模块本身)。
关于julia - Julia 中模块和结构之间的名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57939373/