I'm confused with types names

data_structures
#1

Imagine I have three files.

file1.jl

module myFirstModule
    struct MyStruct
        attribute :: String
    end
end

file2.jl

module mySecondModule
    include("file1.jl")
    include("file3.jl")
    myThirdModule.hello(myFirstModule.MyStruct("a"))
end

file3.jl

module myThirdModule
    include("file1.jl")
    function hello(arg)
        println("argument : ", arg)
        println("isa test : ", isa(arg, myFirstModule.MyStruct))
    end
end

Well, there, the second module creates an instance of myStruct, and calls the “hello” function from the third module with that instance as an argument.

That is the console output :

argument : Main.mySecondModule.myFirstModule.MyStruct("a")
isa test : false

As you can see, the result of the isa test is false .

This acutally means that arg 's type isn’t myFirstModule.MyStruct , but when I print arg , as you can see I’m getting this : Main.mySecondModule.myFirstModule.MyStruct("a")

Does it mean that myFirstModule.MyStruct != Main.mySecondModule.myFirstModule.MyStruct ?

0 Likes

How to arrange a package with submodules and multiple files?
Error with same type called from different modules
#2

These are 2 different modules. Here is a MWE:

julia> module A
           module B end
           module C
               module B end
           end
       end
Main.A

julia> A.B == A.C.B
false

Just because they have the same name and content, doesn’t make them the same module.

2 Likes

#3

Yes. You should in 99.999% of the cases never include the same file twice. Include the files in the top module and refer to types from there.

2 Likes

#4

I can’t see how you would refer to types of a top module without including it.
I mean, if you have more than 3 modules, it would give something like that : Main.mainf.X.X1.X2.X3

0 Likes

#5

Yes.
Due to the fact you’ve included it.
You will also find that

myFirstModule != mySecondModule.myFirstModule.


I think you’ve got off on the wrong track with how modules and includes work.
Hopefully someone can explain it, well. (alas not me as I must sleep)

1 Like

#6

Details about how modules work are given in the relevant chapter of the documentation: Modules. Of particular interest to you here is the paragraph about relative and absolute paths.

Elaborating on your initial example (which I’ve re-written to use shorter names):

module A
    struct MyStruct end

    # C is used by B, so should be defined before
    module C
        using ..A

        function hello(x)
            @show x
            @show x isa A.MyStruct
            nothing
        end
    end

    # B uses both its "parent" A and its "sibling" C
    module B
        using ..A
        using ..C

        hello() = C.hello(A.MyStruct())
    end
end
julia> A.B.hello()
x = Main.A.MyStruct()
x isa A.MyStruct = true
1 Like