# Avoiding redefinition of structs

**URL:** <https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329>\
**Category:** New to Julia\
**Created:** [September 3, 2019, 3:13pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329 "2019-09-03T15:13:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [September 3, 2019, 3:13pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329/1 "2019-09-03T15:13:24Z")

</div>

I have a basic question about including .jl files that I have created myself. I have a file `BinaryTree.jl` which stores a struct BinaryTree and some associated routines. In whichever file I have to use this data structure, I have included the command  
`include("BinaryTree.jl");`  
However, in more than one such file is run, this command is executed more than once, and I get the error

`LoadError: invalid redefinition of constant BinaryTree`

Could you suggest how I can avoid **redefinition**? I don’t see an option but to include `BinaryTree.jl` in every file where it is needed, as this inclusion has to be independent of whether other files have included this data structure or not.

On a related note, is there a way of avoiding repeated loading of routines if a file, say for example `f.jl`, is included more than once during runtime ?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [September 3, 2019, 3:19pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329/2 "2019-09-03T15:19:52Z")

</div>

You shouldn’t have `include("BinaryTree.jl")` everywhere it gets used. You should consider using a [module](https://docs.julialang.org/en/v1/manual/modules/) for it. Alternatively, if you simply put `include("BinaryTree.jl")` somewhere near the beginning of your module or script or whatever you’re working on, you shouldn’t need to worry about it.

---

<div class="post-metadata">

**Author:** ![korsbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/korsbo/32/15942_2.png) [@korsbo](https://discourse.julialang.org/u/korsbo)\
**Post date:** [September 3, 2019, 3:32pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329/3 "2019-09-03T15:32:29Z")

</div>

I agree with @ExpandingMan that you should probably use a module instead. But if you still want a quick-fix, you could use something like

```julia
@isdefined(BinaryTree) || include("BinaryTree.jl")

```

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [September 3, 2019, 3:32pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329/4 "2019-09-03T15:32:57Z")

</div>

> [@ExpandingMan](#):
>
> You shouldn’t have `include("BinaryTree.jl")` everywhere it gets used. You should consider using a [module](https://docs.julialang.org/en/v1/manual/modules/) for it. Alternatively, if you simply put `include("BinaryTree.jl")` somewhere near the beginning of your module or script or whatever you’re working on, you shouldn’t need to worry about it.

Thank you very much, I will read about this structure.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [September 3, 2019, 3:33pm UTC](https://discourse.julialang.org/t/avoiding-redefinition-of-structs/28329/5 "2019-09-03T15:33:34Z")

</div>

> [@korsbo](#):
>
> I agree with @ExpandingMan that you should probably use a module instead. But if you still want a quick-fix, you could use something like
> 
> ```julia
> @isdefined(BinaryTree) && include("BinaryTree.jl")
> 
> ```

Thanks !
