JLD2 Type Reconstruction: Getting an Instance for an arbitrary Type

Hi all,

I recently tagged the v0.3.0 release for JLD2 that changed the internals to
allow for defining custom serialization.
Annoyingly I introduced a bug in the process.
Previously type instantiation was done within @generated functions with something like
Expr(:new, T, fields...).

To make custom serialization possible I had to remove the @generated
changed that to T(fields...) which of course can’t always work. Either the constructor might have been overloaded and e.g. functions can’t be instantiated like that either.

The relevant issue is https://github.com/JuliaIO/JLD2.jl/issues/258
and PR are https://github.com/JuliaIO/JLD2.jl/pull/261
https://github.com/JuliaIO/JLD2.jl/pull/260

The only thing that I could come up with so far that fixes most problems was to do eval(Expr(:new, T, fields))
but that feels wrong.

Any suggestions ?

Best, Jonas

Can you make use of the same trick as BSON does?
https://github.com/JuliaIO/BSON.jl/blob/master/src/extensions.jl#L104

1 Like

That seems to work as well but I don’t fully understand how this works either.