# JLD2 Type Reconstruction: Getting an Instance for an arbitrary Type

**URL:** <https://discourse.julialang.org/t/jld2-type-reconstruction-getting-an-instance-for-an-arbitrary-type/50850>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [November 27, 2020, 8:34am UTC](https://discourse.julialang.org/t/jld2-type-reconstruction-getting-an-instance-for-an-arbitrary-type/50850 "2020-11-27T08:34:42Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [November 27, 2020, 8:34am UTC](https://discourse.julialang.org/t/jld2-type-reconstruction-getting-an-instance-for-an-arbitrary-type/50850/1 "2020-11-27T08:34:42Z")

</div>

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](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/261)  
[https://github.com/JuliaIO/JLD2.jl/pull/260](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

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 27, 2020, 2:21pm UTC](https://discourse.julialang.org/t/jld2-type-reconstruction-getting-an-instance-for-an-arbitrary-type/50850/2 "2020-11-27T14:21:55Z")

</div>

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

---

<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:** [November 27, 2020, 3:37pm UTC](https://discourse.julialang.org/t/jld2-type-reconstruction-getting-an-instance-for-an-arbitrary-type/50850/3 "2020-11-27T15:37:31Z")

</div>

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