# Using struct defined in a different module results in an error

**URL:** <https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603>\
**Category:** New to Julia\
**Tags:** struct, modules\
**Created:** [March 28, 2022, 10:07am UTC](https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603 "2022-03-28T10:07:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J4Q8](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@J4Q8](https://discourse.julialang.org/u/J4Q8)\
**Post date:** [March 28, 2022, 10:07am UTC](https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603/1 "2022-03-28T10:07:36Z")

</div>

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”

```julia
module Trees

export Tree

mutable struct Tree
    connective::Char
    left::Tree
    right::Tree

    # Root constructor
    Tree(symbol) = new(symbol)
end

end #module

```

“parser.jl”

```julia
module Parser

export parseFormula

include("trees.jl")

using .Trees

function parseFormula(a::Char)
    return Tree(a)
end

end #module

```

“interface.jl”

```julia
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:

```julia
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:

1. Why is Julia doing this?

2. How can I make julia treat every `Trees.Tree` as equivalent?

Any general tips on using modules will also be appreciated!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 28, 2022, 11:25am UTC](https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603/2 "2022-03-28T11:25:24Z")

</div>

Hi!  
From what I can tell, the source of your problems is that you include `trees.jl` twice: once within `Interface` and once within `Interface.Parsers`. Remove either of those two and you should end up with only one definition.  
As a side note, many Julia packages (including fairly large ones) actually don’t use submodules at all, and put all of their `include` statements in the root source file. I’m guessing that would make your life even easier

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 28, 2022, 2:38pm UTC](https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603/3 "2022-03-28T14:38:27Z")

</div>

Welcome to our community! 🎊

I recommend you reading this previous but recent thread.

> [@Referencing the same module from multiple files](https://discourse.julialang.org/t/referencing-the-same-module-from-multiple-files/77775/2):
>
> Welcome to our community! confetti_ball This problem is frequent among novice Julia programmers and was already asked multiple times in this forum. (But I concede that the search utility of Discourse never finds any useful results, even when I know they exist.) See: Basically, the rule is: include once and import whenever necessary. Your topmost module should include all files a single time and you should use import/using relative to it (using .MyModule refers to a module defined in th…

---

<div class="post-metadata">

**Author:** ![J4Q8](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@J4Q8](https://discourse.julialang.org/u/J4Q8)\
**Post date:** [March 30, 2022, 9:50am UTC](https://discourse.julialang.org/t/using-struct-defined-in-a-different-module-results-in-an-error/78603/4 "2022-03-30T09:50:26Z")

</div>

It works now! Thank you both for your help!
