# How to save a structure instance (inside module) in JLD2, to load it later correctly?

**URL:** https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339
**Category:** General Usage
**Tags:** question, jld2
**Created:** [March 23, 2022, 3:31pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339 "2022-03-23T15:31:55Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 23, 2022, 3:31pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/1 "2022-03-23T15:31:55Z")

</div>

What should I add in `initilizeFile.jl` to make `readingFile.jl` gives the type of `Sim` as `Simulation` not `JLD2.ReconstructedTypes.var"##Main.readApp.Simulation#292"(2.0, 4.0)` with warning?  
`initilizeFile.jl` is:

```julia
module readApp
using JLD2
Base.@kwdef mutable struct Simulation 
    dt::Float64 = 1e-6
    tmax::Float64 = 32*1e-3
end
function appFunction()
Sim = Simulation(2,4);
save("$(@ __DIR__ )/filename.jld2", "Sim", Sim)
end
appFunction();
end

```

`readingFile.jl`

```julia
using JLD2
Base.@kwdef mutable struct Simulation 
    dt::Float64 = 1e-6
    tmax::Float64 = 32*1e-3
end
Sim = load("$(@ __DIR__ )/filename.jld2", "Sim");
#Now I have warning
┌ Warning: type Main.readApp.Simulation does not exist in workspace; reconstructing
└ @ JLD2 C:\Users\amroa\.julia\packages\JLD2\k9Gt0\src\data\reconstructing_datatypes.jl:461
julia> typeof(Sim)
JLD2.ReconstructedTypes.var"##Main.readApp.Simulation#292"(2.0, 4.0)
# I need to remove the warning and to make typeof(Sim) gives Simulation as below
julia> typeof(Sim)
Simulation

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 23, 2022, 9:24pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/2 "2022-03-23T21:24:00Z")

</div>

Perhaps you need to `using .readApp`:

```julia
julia> module readApp
              using JLD2
              Base.@kwdef mutable struct Simulation
              dt::Float64 = 1e-6
              tmax::Float64 = 32*1e-3
              end
              function appFunction()
              Sim = Simulation(6,7);
              save("$(@ __DIR__ )/filename.jld2", "Sim", Sim)
              end
              appFunction();
       end
Main.readApp

julia> using JLD2, .readApp

julia> Sim = load("$(@ __DIR__ )/filename.jld2", "Sim")
Main.readApp.Simulation(6.0, 7.0)

```

Above code snippet is a single new REPL session.  
Does this help?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 23, 2022, 9:34pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/4 "2022-03-23T21:34:11Z")

</div>

I don’t have much to say about it, except that I don’t understand

```julia
using base

```

It should be

```julia
using JLD2

```

I guess. With that it works for me.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 23, 2022, 9:37pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/5 "2022-03-23T21:37:40Z")

</div>

I am sorry, I have corrected the type of `using base` to `using JLD2` and rephrased my issue above.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 24, 2022, 9:02am UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/6 "2022-03-24T09:02:26Z")

</div>

It’s better not to edit the former posts, because the thread becomes hard to understand for people coming later back to the discussion. Answers start to make no sense anymore.

To your question, two ideas I have about this.  
As already said, first idea, you can just use

```julia
using readApp

```

in `readingFile.jl`, but with that you will read in a `Main.readApp.Simulation` struct, which isn’t the same as a `Main.Simulation` struct. If this isn’t possible for you, which may be another issue, perhaps the real underlying issue of your issue here, the second idea, which comes into my mind is:

To bring `readApp.Simulation` into the `Main` scope by using `eval(m::Module, x)`:

```julia
julia> module readApp
              using JLD2
              expr= :( Base.@kwdef mutable struct Simulation
                                dt::Float64
                                tmax::Float64
                            end )
              Base.eval( Main, expr )
              function appFunction()
                     Sim = Main.Simulation(2,4);
                     save("$(@ __DIR__ )/filename.jld2", "Sim", Sim)
              end
              appFunction();
       end
Main.readApp

```

```julia

julia> using JLD2

julia> Base.@kwdef mutable struct Simulation
           dt::Float64 = 1e-6
           tmax::Float64 = 32*1e-3
       end
Simulation

julia> Sim = load("$(@ __DIR__ )/filename.jld2", "Sim")
Simulation(2.0, 4.0)

julia> typeof(Sim)
Simulation

```

I am not an expert with expressions, macros and eval, but my guess is, there is a good reason, why `eval(m::Module, x)` isn’t exported from `Base`, so there may be some unexpected side effects with this solution.

Perhaps we are talking about a [X-Y-Problem](https://en.wikipedia.org/wiki/XY_problem)?

And of course, having two times the same definition of `struct Simulation` at different locations is considered to be a code smell and error prone.

---

<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: [March 24, 2022, 10:50am UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/7 "2022-03-24T10:50:05Z")

</div>

A working answer should already be here:

> <https://github.com/JuliaIO/JLD2.jl/issues/390>
>
> I was using the below code to create .jld2 file containing an instance Sim of st…ructure Simulation
> \`\`\`julia
> include("./SimulationStructure.jl");
> function appFunction()
> Sim = Simulation();
> io = open("$(@\_\_DIR\_\_)/generatedElements.jld2", "w")
> close(io)
> save("$(@\_\_DIR\_\_)/generatedElements.jld2", "Sim", Sim, "Simulation", Simulation)
> end
> appFunction();
> \`\`\`
> in which SimulationStructure.jl:
> \`\`\`julia
> using base
> export Simulation 
> Base.@kwdef mutable struct Simulation 
> dt::Float64 = 1e-6
> tmax::Float64 = 32\*1e-3
> end
> \`\`\`
> Then, I was reading the created .jld2 by the below code called readFile.jl. and It was working without any warning, in which typeof Sim is Simulation
> \`\`\`julia
> include("./SimulationStructure.jl");
> Sim, Simulation = load("$(@\_\_DIR\_\_)/generatedElements.jld2", "Sim", "Simulation");
> julia\> typeof(Sim)
> Simulation
> \`\`\`
> Now, I have built executable for appFunction() as below and it works fine to generate the .jld2 file.
> \`\`\`julia
> module readApp
> 
> Base.@ccallable function julia\_main()::Cint
> try
> appFunction()
> catch
> Base.invokelatest(Base.display\_error, Base.catch\_stack())
> return 1
> end
> return 0
> end
> 
> Base.@kwdef mutable struct Simulation 
> dt::Float64 = 1e-6
> tmax::Float64 = 32\*1e-3
> end
> 
> function appFunction()
> Sim = Simulation();
> save("$(@\_\_DIR\_\_)/generatedElements.jld2", "Sim", Sim, "Simulation", Simulation)
> end
> end
> \`\`\`
> However, calling readFile.jl to read the generated .jld2 gives this warning and typeof Sim is Simulation
> \`\`\`julia
> ┌ Warning: type readApp.Simulation does not exist in workspace; reconstructing
> └ @ JLD2 C:\\Users\\amroa\\.julia\\packages\\JLD2\\k9Gt0\\src\\data\\reconstructing\_datatypes.jl:461
> julia\> typeof(Sim)
> JLD2.ReconstructedTypes.var"##readApp.Simulation#292"
> julia\> Simulation
> JLD2.UnknownType{String}("testtest.Simulation", #undef)
> \`\`\`
> How can I get ride of the warning and fix the typeof(Sim) to be of Simula JLD2 to be of Simulation?tion?

also:

> [@Warning: type does not exist in workspace; reconstructing](https://discourse.julialang.org/t/warning-type-does-not-exist-in-workspace-reconstructing/78232):
>
> I was using the below code to create .jld2 file containing an instance Sim of structure Simulation include("./SimulationStructure.jl"); function appFunction() Sim = Simulation(); io = open("$(@\_\_DIR\_\_)/generatedElements.jld2", "w") close(io) save("$(@\_\_DIR\_\_)/generatedElements.jld2", "Sim", Sim) end appFunction(); in which SimulationStructure.jl: using base export Simulation Base.@kwdef mutable struct Simulation dt::Float64 = 1e-6 tmax::Float64 = 32\*1e-3 end Then, I was reading the…

@Amro: please avoid double posting (at least add cross references) to reduce confusion and double work from people trying to help.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 24, 2022, 11:35am UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/8 "2022-03-24T11:35:15Z")

</div>

Triple posted? Really?

@JonasIsensee have a look: [Reconstructing warning when dealing with Module?](https://discourse.julialang.org/t/reconstructing-warning-when-dealing-with-module/78363)

Well , 3 people involved, at least, what I can do is spreading some likes…

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 24, 2022, 12:28pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/9 "2022-03-24T12:28:36Z")

</div>

With the github issue it was quadruple posted. Assuming that it is the same OP.

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 24, 2022, 1:45pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/10 "2022-03-24T13:45:26Z")

</div>

Thanks for this, it works. However, I am having several structures and I create instances from them based on several scenarios. So, can I play on the instance directly in `readingFile.jl`, such as:

```julia
module readApp
using JLD2
Base.@kwdef mutable struct Simulation
  dt::Float64
  tmax::Float64
end
function appFunction()
Sim = Simulation(2,4);
expr= :( Sim )
Base.eval( Main, expr )
save("$(@ __DIR__ )/filename.jld2", "Sim", Sim)
end
appFunction();
end
ERROR: UndefVarError: Sim not defined

```

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [March 24, 2022, 1:59pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/11 "2022-03-24T13:59:59Z")

</div>

This would be:

```julia
julia> module readApp
                     using JLD2
                     Base.@kwdef mutable struct Simulation
                       dt::Float64
                       tmax::Float64
                     end
                     function appFunction()
                     expr= :( Sim = Main.readApp.Simulation(2,4) )
                                   Base.eval( Main, expr )
                     save("$(@ __DIR__ )/filename.jld2", "Sim", Main.Sim)
                     end
                     appFunction();
              end
Main.readApp

```

My recommendation is to not follow this path, unless you have read and understood every aspect of:  
[https://docs.julialang.org/en/v1.7/manual/variables-and-scoping/](https://docs.julialang.org/en/v1.7/manual/variables-and-scoping/)  
and  
[https://docs.julialang.org/en/v1.7/manual/modules/](https://docs.julialang.org/en/v1.7/manual/modules/)

---

<div class="post-metadata">

### Author: ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)
#### Post date: [March 24, 2022, 2:11pm UTC](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339/12 "2022-03-24T14:11:19Z")

</div>

Thank you and I will follow your suggestion by re-reading the documentations.

Regarding my problem, my actual code includes building a vector of structure, as below.

```julia
module readApp
                     using JLD2
                     Base.@kwdef mutable struct Simulation
                       dt::Float64=2
                       tmax::Float64=3
                     end
                     function appFunction()
                      Sim = Simulation[]
                      push!(Sim, Simulation(2,4)) # number of pushes varies based on the run time
                      Base.eval( Main, expr )
                     save("$(@ __DIR__ )/filename.jld2", "Sim", Main.Sim)
                     end
                     appFunction();
              end

```

Is it possible to use `expr= :( )` on the vector `Sim` eventually after it is pushed?
