# 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:** 1\
**Showing post:** 7

<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.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-save-a-structure-instance-inside-module-in-jld2-to-load-it-later-correctly/78339)._
