Hello all!
Many thanks in advance for all your answers. I am a bit confused with the types defined in a module.
I am constructing two modules A and B, each of which has two defined types, say Type A and Type B, respectively. Initialization of Type A requires type B. Here is a simple code to generate it
the file “TestModuleA.jl” (edited…)
module A
include("TestModuleB.jl")
type TypeA
b::B.TypeB
function TypeA(args...)
obj = new(B.TypeB())
# assuming args would contain name-value argument pair
length(args)>0? setfield!(obj,Symbol(args[1]),args[2]):nothing
return obj
end
end
end
and the file “TestModuleB.jl”
module B
type TypeB
b::Int64
function TypeB(args...)
new(0)
end
end
end
Now, in the REPL
Main> include("TestModuleA.jl"), include("TestModuleB.jl")
Initialization of A goes smoothly with default field value
Trying to initialize a new Type A, with say new field of TypeB.b=9 (instead of the default 0)
Main> A.TypeA("b",B.TypeB(9))
I get the error
ERROR: TypeError: setfield!: expected A.B.TypeB, got B.TypeB
however,
Main> typeof(B.TypeB(9))
B.TypeB
as expected.
Why is the type of TypeB changed to A.B.TypeB? why is it not the expected B.TypeB?
as I wrote the lines above I discovered that
Main> A.TypeA("b",A.B.TypeB(9))
would do the job, however, the syntax is a bit awkward, specifically if I want to initialize a new TypeA by a Type B created by a third module, or another piece of code.
many thanks!