Creating structures while writing code

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

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:

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

I get the error message:

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.

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

2 Likes

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

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

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

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.

2 Likes

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.

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