Hi!
I am new to julia, so I wanted to try out some code. Everything was nice and clear until I started using multiple scripts. I simplified them to necessary minimum to reproduce error that is bugging me.
“trees.jl”
module Trees
export Tree
mutable struct Tree
connective::Char
left::Tree
right::Tree
# Root constructor
Tree(symbol) = new(symbol)
end
end #module
“parser.jl”
module Parser
export parseFormula
include("trees.jl")
using .Trees
function parseFormula(a::Char)
return Tree(a)
end
end #module
“interface.jl”
module Interface
export foo
include("parser.jl")
include("trees.jl")
#include("solver.jl")
using .Parser
using .Trees
function foo(a::Char = 'a')
initlist = Tree[]
push!(initlist, parseFormula(a))
end
end #module
Given these 3 files, you can run using .Interface and foo(), which will produce the following error:
ERROR: MethodError: Cannot `convert` an object of type Main.Interface.Parser.Trees.Tree to an object of type Main.Interface.Trees.Tree
To me this is a weird behaviour, because these two classes are exactly the same since they are defined in “trees.jl”. However, julia for some reason treats the Tree defined in “parser.jl” as Main.Parser.Trees.Tree, and Tree defined in “interface.jl” as Main.Interface.Trees.Tree. If that wasn’t enough when I call parseFormula from within “interface.jl” the Tree becomes Main.Interface.Parser.Trees.Tree.
I figured out that I can get rid of the error by changing initlist = Tree[] to initlist = Parser.Tree[], but to me this seems like a bad practice. So I would like to know:
-
Why is Julia doing this?
-
How can I make julia treat every
Trees.Treeas equivalent?
Any general tips on using modules will also be appreciated!
Thanks in advance!