Julia 中模块和结构之间的名称冲突

M.E*_*.E. 4 julia

我想确认我不能Test在定义为Test.

文件main.jl

# MAIN PROGRAM

include("test.jl")
import .Test

t = Test.Test(3,4)
println(t)
Run Code Online (Sandbox Code Playgroud)

文件test.jl

module Test

        struct Test
            a::Int
            b::Int
        end

end
Run Code Online (Sandbox Code Playgroud)

我得到:

$ /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
Run Code Online (Sandbox Code Playgroud)

fre*_*kre 5

我想确认我不能在定义为 Test 的模块中定义名为 Test 的结构。

你不能。每个模块都有一个自绑定,因此在Test模块内部Test已经使用了名称(由模块本身)。

  • 当您想要一个具有“main”名称的结构时,有时包/模块名称是复数。例如,可以通过加载“Colors”包来获取“Color”类型。 (2认同)