# Creating structures while writing code

**URL:** <https://discourse.julialang.org/t/creating-structures-while-writing-code/40895>\
**Category:** New to Julia\
**Created:** [June 6, 2020, 10:19pm UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895 "2020-06-06T22:19:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 6, 2020, 10:19pm UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/1 "2020-06-06T22:19:30Z")

</div>

I am working with my own structs for the first time and am having some conceptual problems. Consider the code

```julia
mutable struct myTest
    arr::Array{Float64, 1} # arr is an array
    myTest(eqs::Array{Float64,1}) = new(eqs)
end

```

This works fine. But now, I change my mind and wish to change the 1D array to a 2D array. So I replace the 1st line of the structure, to get the new structure:

```julia
mutable struct myTest
    arr::Array{Float64, 2} # arr is an array
    myTest(eqs::Array{Float64,2}) = new(eqs)
end

```

I get the error message:

```julia
Invalid redefinition of constant myTest

```

So how does one create structs in experimental mode. I cannot make the correct decision on the first shot. I also created this structure within a module and got the same message. I would like to know how to change the type within the structure in order to “try” something out.

I understand the super optimization of Julia. What I do not understand is why there cannot be a “code construction mode” that is not optimized and which allow the coders more freedom. When the code is to be tested, the coder could flip some switch and return to the current way of doing things. This is very frustrating and there is a very steep learning curve. I have never worked with such a cool language and at the time with a language with these kinds of “apparent” limitations. I have not yet found any document or book that explains how to handle this situation.

---

<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:** [June 6, 2020, 10:25pm UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/2 "2020-06-06T22:25:25Z")

</div>

[https://github.com/timholy/Revise.jl/issues/18](https://github.com/timholy/Revise.jl/issues/18)  
I know, this is not the solution, but it provides a background.

---

<div class="post-metadata">

**Author:** ![virtualgraham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/virtualgraham/32/15200_2.png) [@virtualgraham](https://discourse.julialang.org/u/virtualgraham)\
**Post date:** [June 6, 2020, 10:25pm UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/3 "2020-06-06T22:25:44Z")

</div>

Here is a good answer from another thread. Basically you can put the definition into a module.

> [@Redefine Struct when working with REPL](https://discourse.julialang.org/t/redefine-struct-when-working-with-repl/25942/2):
>
> Personally I’ve found that my structures don’t change too often, mostly it’s the code that is changing, so when I’ve (rarely) needed to change the structures I just do a stop/start of Julia inside atom. If your structures are changing often then you can place them in a module like: module Data struct Lion maneColor::String roar::String weight::Int # Do some work and then realize I need this attribute end end When you rerun the script you will get a warning that…

```julia
module myModule
   mutable struct myTest
       arr::Array{Float64, 1} # arr is an array
       myTest(eqs::Array{Float64,1}) = new(eqs)
   end
end

```

> [@Redefine Struct when working with REPL](https://discourse.julialang.org/t/redefine-struct-when-working-with-repl/25942/2):
>
> When you rerun the script you will get a warning that Data was redefined, which you can ignore. However Data.Lion will now be the new data object.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 6, 2020, 10:28pm UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/4 "2020-06-06T22:28:38Z")

</div>

One option that might help is designing parametric types that are more flexible in the first place. For example, here you could have defined

```julia
mutable struct myTest{T,N}
    arr::Array{T, N} # arr is an array
    myTest(eqs::Array{T,N}) = new(eqs)
end

```

This type is very similar, but will work for any element type or dimmension number.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 7, 2020, 2:18am UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/5 "2020-06-07T02:18:45Z")

</div>

Thank you all for your advice! I finally decided to put my structs inside a module, and use an include along with full path naming of the structures. This allows me to make the changes I want. So using Revise.jl would probably be advisable, but my first attempts failed, and I would like to make progress on the real code. So I am ok for now.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [June 7, 2020, 5:01am UTC](https://discourse.julialang.org/t/creating-structures-while-writing-code/40895/6 "2020-06-07T05:01:09Z")

</div>

I sometimes will make `myTest`, then `myTest1`, then `myTest2` as I add features to avoid restarting.
