JLD2.jl icon indicating copy to clipboard operation
JLD2.jl copied to clipboard

Bad saving of interactively-defined function in tuple

Open cstjean opened this issue 8 years ago • 2 comments

The function disappears from the tuple:

julia> using JLD2, FileIO

julia> f(x) = x+1
f (generic function with 1 method)

julia> save("test.jld2", "tuple", (f, :hello))

### New session

julia> using JLD2, FileIO

julia> load("test.jld2", "tuple")
(:hello,)

cstjean avatar Sep 03 '17 03:09 cstjean

JLD2 does not currently attempt to serialize functions in a way that they can be reconstructed without the source having been loaded (see #13). But this should probably print a warning if f is undefined in the workspace instead of silently dropping it from the tuple.

simonster avatar Sep 03 '17 04:09 simonster

I expected it to be a ReconstructedType instead of dropped, as in:

using JLD2, FileIO

struct Foo
a
b
c
end

f() = 2

save("test.jld2", "val", Foo(vcat, f, Any[vcat, f]))
using JLD2, FileIO

struct Foo
a
b
c
end

load("test.jld2", "val")

> Foo(vcat, JLD2.ReconstructedTypes.###f#665(), Any[vcat, JLD2.ReconstructedTypes.###f#665()])

Another issue, perhaps unsolvable: saving Function[vcat, f] has a problem on reload, because ReconstructedTypes are not Function objects.

The ReconstructedTypes behaviour is great for saving and loading traces in TraceCalls.jl, thank you for JLD2. 👍

cstjean avatar Sep 03 '17 11:09 cstjean