Skip to content

Commit

Permalink
Fix error when trying to store instances of <: Function types
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelBadr committed Sep 20, 2024
1 parent b54623f commit be2b913
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/data/writing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ end

function h5convert!(out::Pointers, ::DataTypeODR, f::JLDFile, T::DataType, wsession::JLDWriteSession)
t = typename(T)
if T <: Function && isgensym(nameof(T.instance))
if T <: Function && isgensym(Symbol(T))
@warn LazyString("Attempting to store ", T, ".\n",
"JLD2 only stores functions by name.\n",
" This may not be useful for anonymous functions.")
Expand Down
14 changes: 13 additions & 1 deletion test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -788,4 +788,16 @@ if VERSION ≥ v"1.7"
f = x -> x+1
@test_warn contains("Attempting to store") save_object(fn, f)
end
end
end

struct Foo601 <: Function
x::Int
end

@testset "Issue #601: Storing <: Function objects" begin
foo = Foo601(2)
tempfile = tempname()
jldsave(tempfile; foo)
loaded_foo = load(tempfile, "foo")
@test loaded_foo isa Foo601
end

0 comments on commit be2b913

Please sign in to comment.