# Julia: Split module into multiple files 

**URL:** <https://discourse.julialang.org/t/julia-split-module-into-multiple-files/13359>\
**Category:** New to Julia\
**Tags:** module\
**Created:** [August 13, 2018, 11:35am UTC](https://discourse.julialang.org/t/julia-split-module-into-multiple-files/13359 "2018-08-13T11:35:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![camminady](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@camminady](https://discourse.julialang.org/u/camminady)\
**Post date:** [August 13, 2018, 11:35am UTC](https://discourse.julialang.org/t/julia-split-module-into-multiple-files/13359/1 "2018-08-13T11:35:42Z")

</div>

I copied this question from StackOverflow. I was not sure if I would get answers there, or if it is better to ask here. Link: [Julia: Split module into multiple files - Stack Overflow](https://stackoverflow.com/questions/51819338/julia-split-module-into-multiple-files) :

I am using Julia v0.6.4. Consider the following module, saved in `FooModule.jl`:

```
module FooModule

export Foo
export doSomething

mutable struct Foo
    x::Int64
    function Foo(xin::Int64)
        new(xin)
    end # Foo
end # struct

function doSomething(F::Foo)
    println(F.x+123)
end # doSomething

end # module

```

And a `main.jl`:

```
using FooModule
function main()
F = Foo(10)
doSomething(F)
end # main

```

Now I start the Julia REPL:

```
julia> include("main.jl");

julia> main()
133

```

I.e. I get what I expect.  
Now imagine `doSomething` contains many lines and I want to split `FooModule.jl`. I would like to do something like this:

```
module FooModule

export Foo
export doSomething

include("doSomething.jl") # <- include instead of writing out

mutable struct Foo
    x::Int64
    function Foo(xin::Int64)
        new(xin)
    end # Foo
end # struct

end # module

```

and `doSomething.jl`

```
function doSomething(F::Foo)
    println(F.x+123)
end # doSomething

```

This gives an error since `doSomething.jl` knows nothing about `Foo`.

```
julia> include("main.jl");
ERROR: LoadError: LoadError: LoadError: UndefVarError: Foo not defined
Stacktrace:
 [1] include_from_node1(::String) at ./loading.jl:576
 [2] include(::String) at ./sysimg.jl:14
 [3] include_from_node1(::String) at ./loading.jl:576
 [4] eval(::Module, ::Any) at ./boot.jl:235
 [5] _require(::Symbol) at ./loading.jl:490
 [6] require(::Symbol) at ./loading.jl:405
 [7] include_from_node1(::String) at ./loading.jl:576
 [8] include(::String) at ./sysimg.jl:14
while loading ~/Desktop/doSomething.jl, in expression starting on line 1
while loading ~/Desktop/FooModule.jl, in expression starting on line 6
while loading ~/Desktop/main.jl, in expression starting on line 1

```

How can I resolve this problem?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 13, 2018, 11:44am UTC](https://discourse.julialang.org/t/julia-split-module-into-multiple-files/13359/2 "2018-08-13T11:44:04Z")

</div>

Put the `include` after the struct definition? It’s pretty typical for modules to have several includes (you might put the struct in a separate file too and `include` that as well), but order matters. You can’t reference something before it’s defined.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [August 13, 2018, 11:53am UTC](https://discourse.julialang.org/t/julia-split-module-into-multiple-files/13359/3 "2018-08-13T11:53:55Z")

</div>

You already got an answer on SO, and you even accepted it long before re-posting the question here. Was there something that needed clarification? Would be good to update the answer there too if that’s the case.
