# What is the preferred way to use multiple files?

**URL:** <https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687>\
**Category:** General Usage\
**Tags:** question, modules, code-organization\
**Created:** [November 4, 2019, 8:35am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687 "2019-11-04T08:35:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![EvgeniyMist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniymist/32/12875_2.png) [@EvgeniyMist](https://discourse.julialang.org/u/EvgeniyMist)\
**Post date:** [November 4, 2019, 8:35am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/1 "2019-11-04T08:35:07Z")

</div>

I wanted to structure my code by dividing it into several files. But I ran into some problem. So, in its simplest form, I have 3 files:  
main.jl

```julia
module main

include("structs.jl")
include("functions.jl")

first_point = structs.Point(1.5, 1.5)
print(functions.distance(first_point))

end

```

functions.jl

```julia
module functions

include("structs.jl")

export distance

function distance(point)
    return (point.x^2+point.y^2)^0.5
end

end

```

structs.jl

```julia
module structs

export Point

mutable struct Point
    x::Float64
    y::Float64
    Point(x, y) = new(x, y)
end

end

```

And when i try to run main file I get this error:  
ERROR: LoadError: MethodError: no method matching distance(::Main.main.structs.Point)  
Closest candidates are: distance(::Main.main.functions.structs.Point)  
So what’s the problem?

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [November 4, 2019, 8:47am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/2 "2019-11-04T08:47:39Z")

</div>

What julia version are you on?  
Have you run your code on a clean julia session?  
On 1.2.0 your code seems to run.

---

<div class="post-metadata">

**Author:** ![EvgeniyMist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniymist/32/12875_2.png) [@EvgeniyMist](https://discourse.julialang.org/u/EvgeniyMist)\
**Post date:** [November 4, 2019, 8:58am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/3 "2019-11-04T08:58:26Z")

</div>

I have Version 1.2.0 (2019-08-20)  
Just now restarted the session - the result has not changed

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [November 4, 2019, 9:12am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/4 "2019-11-04T09:12:13Z")

</div>

You are including structs.jl twice. Is that really what you want?

---

<div class="post-metadata">

**Author:** ![EvgeniyMist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniymist/32/12875_2.png) [@EvgeniyMist](https://discourse.julialang.org/u/EvgeniyMist)\
**Post date:** [November 4, 2019, 9:16am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/5 "2019-11-04T09:16:00Z")

</div>

So I should replace my main file by something like that?

```julia
module main

include("functions.jl")

first_point = functions.structs.Point(1.5, 1.5)
print(functions.distance(first_point))

end

```

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [November 4, 2019, 9:17am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/6 "2019-11-04T09:17:04Z")

</div>

Alternatively remove the `include` statement from `functions.jl`  
and only include `structs.jl` from main.

That depends a little bit on your desired program structure.  
Do you want doubly nested modules by making `structs` a submodule of `functions` which itself lives in `main`?

Alternatively both `functions` and `structs` could live in `main` directly.

Note however that all these modules are not necessary at all in your case here. In particular your `main` module does not seem to serve an purpose.

---

<div class="post-metadata">

**Author:** ![improbable22](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/improbable22/32/5464_2.png) [@improbable22](https://discourse.julialang.org/u/improbable22)\
**Post date:** [November 4, 2019, 10:15am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/7 "2019-11-04T10:15:51Z")

</div>

Maybe you know this, but Julia doesn’t care what files your code lives in, nor what folders those live in, `include` is just like pasting the code into that file. (I forget whether there are edge cases, but this is the rough idea.) So you can split or combine them in whatever way you like.

But it does care about `module` a lot. And generally you shouldn’t be making sub-modules within other modules without a good reason. You appear to have two sub-modules `main.structs` and `main.functions.structs` which just happen to be defined with identical source code.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [November 4, 2019, 10:17am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/8 "2019-11-04T10:17:32Z")

</div>

I’ve ran into the same problem.  
My solution was to make `Functions` and `Structs` modules aware of each other.  
main.jl:

```julia
module main

include("structs.jl")
include("functions.jl")

...
end # module

```

structs.jl:

```julia
module structs

using ..functions : ...

...
end # module

```

functions.jl:

```julia
module functions

using ..structs : ...

...
end # module

```

That way, `functions` loads `structs` module from the `main` namespace, and the error goes away. The downside is, of course, that modules `functions` and `structs` cannot be used in a stand-alone fashion.

---

<div class="post-metadata">

**Author:** ![EvgeniyMist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniymist/32/12875_2.png) [@EvgeniyMist](https://discourse.julialang.org/u/EvgeniyMist)\
**Post date:** [November 4, 2019, 10:48am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/9 "2019-11-04T10:48:17Z")

</div>

I think, that I misunderstand, when there is necessary to create modules, because I have not met this in other languages. With the help of modules I can hide some functions from user. I have only one analogy - private methods and private attributes in OOP languages, but it isn’t good analogy

---

<div class="post-metadata">

**Author:** ![EvgeniyMist](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evgeniymist/32/12875_2.png) [@EvgeniyMist](https://discourse.julialang.org/u/EvgeniyMist)\
**Post date:** [November 4, 2019, 10:56am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/10 "2019-11-04T10:56:57Z")

</div>

If I am not mistaken in C ++ include works in the same way

---

<div class="post-metadata">

**Author:** ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)\
**Post date:** [November 4, 2019, 11:26am UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/11 "2019-11-04T11:26:40Z")

</div>

Yeah, this could be the case.  
In my eyes modules are never necessary and in julia there is no way to truly _hide_ functions from a user.  
One typically `export`s public API but one can always access nonexported functions via `MyModule.functionname`.

Modules can be used to group various function/type definitions into a (potentially) separate name space  
that allows you to easily include and reuse them.

As an example: In research projects I have one module that contains all my utility function definitions and machinery that I use in multiple places. My scripts load that module and work from there.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [November 4, 2019, 12:03pm UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/12 "2019-11-04T12:03:56Z")

</div>

In julia, as a rule, don’t use submodules.

Namespaces are overrated, lets do less of those.  
It is a bit of a mind-twist, I know.  
Took me a while to get use to it.

Goal is to seperate functionality that is independent.  
If it is truely independent, then it can be its own package.  
If it isn’t then it can just go in the main module of the package.  
Submodules work really poorly if the code isn’t independent, as importing things from the parent or sibling namespaces has to be done explictly.

Some valid uses of submodules modules:

1. You think it is independent, but you are not sure yet. Then a submodule lets you try it out. If you can indeed do it without suffering and importing from the parent namespace then it is independent, and you can later make a new package for it when you have time.
2. as a hack to allow all your enums to be referred to as `EnumMod.enumval`, by just putting the `@enum` as the only thing inside `module EnumMod`.
3. You are writing julia itself and making a submodule of `Base`, thus your parent module is inscope because it is `Base` and `Base`’s exports are always inscope. Also you don’t have the option of a standalone package (or even a stdlib) as `Base` can’t depend on those.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [November 4, 2019, 9:25pm UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/13 "2019-11-04T21:25:33Z")

</div>

> [@oxinabox](#):
>
> If it is truely independent, then it can be its own package.

While I agree with most of what you say, I’m not sure about this bit. Well, I do agree that independent sets of features _can_ go into separate packages, but why would they _have to_? If a given piece of code provides functionality that is independent, but so specific that it is only ever going to be used in a single application, why bother separate it into a package?

Developing two packages at the same time, one depending on the other, is much more cumbersome IMO than having two submodules in the same package, one `using` or `import`ing the other. For example, when there is only one package, there is only one set of sources in a git repository, which means you don’t have to worry about which specific version of your application package depends on which specific version of your library package.

  

> [@oxinabox](#):
>
> You think it is independent, but you are not sure yet. Then a submodule lets you try it out. If you can indeed do it without suffering and importing from the parent namespace then it is independent, and you can later make a new package for it when you have time.

I fully agree with that, but I would add: “you can later make a new package for it when you have time _and you have a use for this set of functionality in an other context_”.

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 25, 2024, 2:38pm UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/14 "2024-10-25T14:38:28Z")

</div>

> [@EvgeniyMist](#):
>
> And when i try to run main file I get this error:  
> ERROR: LoadError: MethodError: no method matching distance(::Main.main.structs.Point)  
> Closest candidates are: distance(::Main.main.functions.structs.Point)  
> So what’s the problem?

I know that others have solved your problem and that this is an old thread, but no one really explained

- how you _should_ solve the problem
- _why_ it works

Instead, you were provided with a few solutions. I assume they work, but they do not address your fundamental problem.

In the code you have shown, you provide an example type and an example function. Those two things are very tightly linked. The two concepts depend on each other, **and** there is an important ordering between them.

Let’s explore this

- first you define a _type_ (a point in 2d space)
- then you defined a function which can operate on that type (something which can calculate the distance to the origin)

Two things to observe:

- the _type_ can exist without the function
- the _function_ cannot exist without the type

Hence we conclude _the function depends on the type_. The type _does not_ depend on your function.

Despite the above, the two are very closely related concepts. This _strongly suggests_ they should be in the same file, and certainly the same module.

Put another way, _would you publish this package with the type by itself_?

The answer is no, because the point type does not have much use without the associated distance-to-origin measuring function. The reason is that you are providing clients of your library a function to measure the distance-to-origin, you are less interested in providing them with a type.

If you follow the rules implied by these design concepts, you will automatically solve the problem by putting everything in a single file.

- _I understand that you chose, deliberatly, to split the function and the type into two files to demonstrate a problem, but in this context it doesn’t make sense for the reasons provided above_

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 25, 2024, 2:53pm UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/15 "2024-10-25T14:53:50Z")

</div>

Your enthusiasm is good, but you should know that reviving an old thread, especially one that has resolved after significant activity, is highly discouraged as a social custom; some discourse forums actually lock threads after a period of inactivity, but Julia’s is a bit more flexible. The usual approach is to start a new thread and link the old thread with a summary; if the intent is to address someone in particular, a private message can be sent. I can also recommend experimenting in the interactive REPL, especially since you started a somewhat related thread about `include` yesterday. This will often prevent basic mistakes such as:

> [@anon91151494](#):
>
> the _function_ cannot exist without the type

To illustrate, we can evaluate said function right into a fresh session without the type. It not only successfully exists, it works on types that weren’t even brought up:

```julia
julia> function distance(point)
           return (point.x^2+point.y^2)^0.5
       end
distance (generic function with 1 method)

julia> using StaticArrays

julia> distance(SVector(1, 2))
2.23606797749979

```

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 25, 2024, 3:06pm UTC](https://discourse.julialang.org/t/what-is-the-preferred-way-to-use-multiple-files/30687/16 "2024-10-25T15:06:59Z")

</div>

> [@Benny](#):
>
> To illustrate, we can evaluate said function right into a fresh session without the type. It not only successfully exists, it works on types that weren’t even brought up:

You missed the point I was making. Here, your function takes an `Any`. It doesn’t take a `Point2D`

I should have been more explicit - what I was suggesting was the function `distance` _should_ have taken some kind of concrete or abstract type which is defined by the example module (package) shown in the first post.

I know that this Euclidian distance measuring function is a generic concept, but for the purpose of the point being made just imagine that it were something else, specific/niche which doesn’t make sense without the accompanying type(s) created in the same package. This is how _most_ packages work - you introduce something _new_ not a generic formula/algorithm.

Of course it _could_ be you write a package of generic algorithms but then what I suggested would not apply for the reasons you gave.
