JuliaIO / JLD.jl

Saving and loading julia variables while preserving native types
MIT License
277 stars 55 forks source link

Incorporating JLD into package, and using custom types from that package #247

Open IanButterworth opened 5 years ago

IanButterworth commented 5 years ago

I'd like to use JLD within a package to save custom types from that same package. What's the correct way to do this? Call the package itself from within addrequire() (which causes an error if I do it as below)? Or should the package in addrequire() be a lighter-weight types-only package? Is there a performance hit if it's the former?

i.e. within the package ThisPackage, this errors

jldopen(filepath, "w") do file
        addrequire(file,ThisPackage)
        write(file, "x", x)
    end
ERROR: UndefVarError: module_parent not defined
Stacktrace:
 [1] _istoplevel(::Module) at C:\Users\IanB\.julia\packages\JLD\1BoSz\src\JLD.jl:1253
 [2] addrequire(::JLD.JldFile, ::Module) at C:\Users\IanB\.julia\packages\JLD\1BoSz\src\JLD.jl:1256
...
IanButterworth commented 5 years ago

Also, is there any reason to not do this with JLD, over HDF5.jl etc.? I'm using JLD as there were examples, and I couldn't immediately find how to use custom types in HDF5

IanButterworth commented 5 years ago

I just realized that the error above is a <1.0 error that's fixed in master. It would be good to push a new release out to catch that @JeffBezanson

virgodi commented 5 years ago

Hi ! Is there a plan to make a tag a out of the latest master ? Facing the same issue...

truedichotomy commented 4 years ago

Have the same issue, gives me ERROR: LoadError: UndefVarError: module_parent not defined error. Test code attached.

debugCode.jl:

using JLD
push!(LOAD_PATH, pwd());

import debugTypes: Type1, Type2
import debugTypes

a = Type1(3.14);
b = Type2(100.0);

filepath = "test_data.jld"
jldopen(filepath,"w") do file
    addrequire(file, debugTypes)
    write(file, "a", a, "b", b)
end

debugTypes.jl:

module debugTypes

using Dates
export Type1, Type2

mutable struct Type1
    var1::Float64
end

struct Type2
    var2::Float64
end

end