# Organizing structs and modules

**URL:** https://discourse.julialang.org/t/organizing-structs-and-modules/34993
**Category:** New to Julia
**Created:** [February 21, 2020, 8:16pm UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993 "2020-02-21T20:16:36Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![squirrel](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@squirrel](https://discourse.julialang.org/u/squirrel)
#### Post date: [February 21, 2020, 8:16pm UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/1 "2020-02-21T20:16:36Z")

</div>

I’m running into some trouble with organizing my files. I want to divide up my functions and structs into separate modules in separate files to keep them somewhat organized, and I don’t want to install them as Packages because I’m too noob. But some of my modules use structs that are defined in and exported from other modules. This is causing incompatibility when I try to use the structs in more than one module.

For example, let’s say `module Fruit` has `struct Seed` and `function germinate()`.

```julia
module Fruit

export Seed, germinate

struct Seed
    # blah...
end

function germinate()
  # blah blah...
end

end

```

Then there is `module Tree` which includes that uses `seed`.

```julia
module Tree

include("Fruit.jl")
using .Fruit

export plant

function plant(s::Seed)
   # more blah...
end

end

```

But then I have another file `Orchard.jl` that wants to include and use both `struct Seed` and `function plant`, and if I make a `Seed` imported from `Fruit`, it won’t be recognized by the function in `Tree` if I pass it as an argument.

```julia
include("Fruit.jl")
using .Fruit
include("Tree.jl")
using .Tree

s = Seed()

plant(s)

```

This causes an error saying that it can’t find the function `plant(::Seed)`.

I’ve been getting around this before by nesting all of my module files (like, Orchard includes Tree which includes Fruit), but this is getting to a point where it doesn’t seem very logical and I’d like to separate them.

Any insight would be very much appreciated.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [February 21, 2020, 8:32pm UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/2 "2020-02-21T20:32:14Z")

</div>

There are quite a few related discussions on discourse, such as [Large programs: structuring modules & include such that to increase performance and readability](https://discourse.julialang.org/t/large-programs-structuring-modules-include-such-that-to-increase-performance-and-readability/29102).

My (personal) suggestion is: unless the project is quite large, don’t use sub-modules at all. It just leads to exactly the kinds of issues that you are running into (how do I organize everything so that `A` depends on `B` but not the other way around? How do I make all types visible everywhere they need to be seen?).

Tastes differ on this question, though.

If you want sub-modules, you need one master module (probably `Orchard` in your case). Then `orchard.jl` `includes` the other modules (don’t include the same file multiple times). And if you need to see `Seed` in `Fruit` you do

```julia
module Orchard

module Seed
 ...
end
using .Seed

module Fruit
  using ..Seed
  ...
end
using .Fruit

end

```

Hope this helps.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [February 21, 2020, 9:55pm UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/3 "2020-02-21T21:55:01Z")

</div>

Here’s another discussion which covers the exact same issue: [Organizing modules. Is it OK to organize a project using several modules in Julia? - #5 by rdeits](https://discourse.julialang.org/t/organizing-modules-is-it-ok-to-organize-a-project-using-several-modules-in-julia/34535/5)

The summary is, as `@hendri54` said, that you should never be `include`-ing the same file more than once. Fortunately, you should also never need to do that if you structure your modules as suggested.

---

<div class="post-metadata">

### Author: ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)
#### Post date: [February 24, 2020, 7:57am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/4 "2020-02-24T07:57:31Z")

</div>

Thanks for clarifying that is the structure I am adopting.

---

<div class="post-metadata">

### Author: ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)
#### Post date: [February 25, 2020, 7:00am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/5 "2020-02-25T07:00:37Z")

</div>

**Limitations of modules.** The Julia manual states: _Note that variable bindings can only be changed within their global scope and not from an outside module._ which to me is problematic. https://michaelhatherly.github.io/julia-docs/en/latest/manual/variables-and-scoping.html

I have a large number of options using sub-modules for clarity (not shown). A simple example :

```julia
module option
     Number_Simulation = 100
end

```

Trying to modify **option.Number\_Simulation**

```julia
module test
     import option

     function TEST()
         if option.Number_Simulation < 10
               option.Number_Simulation = 10
          end
    end
end

```

Nevertheless I get the following error: _ERROR: cannot assign variables in other modules_ although I did not declare _Number\_Simulation_ as a **const**. Is there a way around to solve this issue? Many thanks for any help you may provide.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 25, 2020, 7:07am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/6 "2020-02-25T07:07:12Z")

</div>

Just expose it via an accessor API. The following is more or less the idiomatic (type stable etc) solution:

```julia
module HasState
const THE_THING = Ref(0)
set_the_thing(value) = THE_THING[] = value
get_the_thing() = THE_THING[]
end

```

---

<div class="post-metadata">

### Author: ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)
#### Post date: [February 25, 2020, 7:11am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/7 "2020-02-25T07:11:29Z")

</div>

Hi Tamas,  
Thanks ever so much for taking your time to answer the question. Since I am still a beginner will you kindly provide to the community a working example, thanks 😀

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 25, 2020, 7:13am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/8 "2020-02-25T07:13:18Z")

</div>

I wonder if you missed the code in my reply, it _is_ a working example.

---

<div class="post-metadata">

### Author: ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)
#### Post date: [February 25, 2020, 7:29am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/9 "2020-02-25T07:29:18Z")

</div>

Tamas is this the solution which you are proposing?

```julia
module option
     Number_Simulation = 100
     set_the_thing(value) = Number_Simulation[] = value
     get_the_thing() = Number_Simulation[]
end

```

Trying to modify **option.Number\_Simulation**

```julia
module test
     import option

     function TEST()
         if option.Number_Simulation < 10
               option.Number_Simulation = 10
          end
    end
end

```

I still get the error message _ERROR: cannot assign variables in other modules_  
Thanks for helping to correct the code.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [February 25, 2020, 7:37am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/10 "2020-02-25T07:37:34Z")

</div>

You are missing key parts of the solution I propose (`Ref`), also, as I suggested, you should use the accessor API to set things, eg

```julia
using HasState
HasState.get_the_thing()
HasState.set_the_thing(4)

```

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [February 25, 2020, 5:14pm UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/11 "2020-02-25T17:14:42Z")

</div>

Or, since you have many options, bundle them in a `struct`:

```julia
module test1

mutable struct T1
	x
	y
end

const t1 = T1(2, 3);

function set_x(z)
	t1.x = z;
end

function get_x()
	return t1.x
end

end

julia> using .test1

julia> test1.set_x(333)
333

julia> test1.t1
Main.test1.T1(333, 3)

```

---

<div class="post-metadata">

### Author: ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)
#### Post date: [February 27, 2020, 2:27am UTC](https://discourse.julialang.org/t/organizing-structs-and-modules/34993/12 "2020-02-27T02:27:57Z")

</div>

Thanks that could be an alternative solution 😃
