# How to write a type to a file?

**URL:** https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798
**Category:** General Usage
**Created:** [June 25, 2026, 8:04am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798 "2026-06-25T08:04:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [June 25, 2026, 8:04am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/1 "2026-06-25T08:04:10Z")

</div>

> **[GitHub - phma/HalftoneFunction.jl: Find the function that makes the perfect halftone...](https://github.com/phma/HalftoneFunction.jl)**
>
> Find the function that makes the perfect halftone screen

I’m writing a program to compute the halftone function, and I need to write a `struct` to a file because it takes a long time to compute. It consists of an `OffsetVector` of any real floating-point type. I’ve figured out how to turn any number between 0 and 1, whether `Float64` or `BigFloat`, into a bytestring which I can write to a file, but I need to write the type to the start of the file, so that I can construct the `HalftoneApprox` when reading it from the file. I could write some `elseif`s checking whether it’s a `Float16`, `Float32`, `Float64`, or `BigFloat`, but if someone ever creates the types `Float80` (which exists in x86(64)) and `Float128` (which exists in POWER), the code will fail. How can I write a type to a file so that I can read it from the file and construct the same type, at least if the architecture has it?

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [June 25, 2026, 8:46am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/2 "2026-06-25T08:46:54Z")

</div>

[Serialization](https://docs.julialang.org/en/v1/stdlib/Serialization/) would help?

---

<div class="post-metadata">

### Author: ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)
#### Post date: [June 25, 2026, 9:36am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/3 "2026-06-25T09:36:02Z")

</div>

You could also print the type and values to a file and then read the contwnts back to a string and parse it to recover the type (and values) with `Meta.parse` .

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 25, 2026, 9:52am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/4 "2026-06-25T09:52:01Z")

</div>

If you only want to support types in Base, the lowtech solution is to simply write the type name in the file and reconstruct it with `getfield(Base, Symbol(type_name))`.

Otherwise you need to look into packages like JLD2 or BSON. The Serialization stdlib can be used but it has a number of caveats when it comes to format stability.

Be aware that most solutions, except the first one, have the potential to execute arbitrary code when parsing the file, which may or may not be a real problem.

> [@zgornel](#):
>
> recover the type (and values) with `Meta.parse`

To nitpick this needs to be combined with `eval`, and then also has the potential to execute arbitrary code.

---

<div class="post-metadata">

### Author: ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)
#### Post date: [June 25, 2026, 11:56am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/5 "2026-06-25T11:56:15Z")

</div>

I’ve done this with JLD2.jl and it works pretty well.

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [June 26, 2026, 1:05am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/6 "2026-06-26T01:05:04Z")

</div>

> [@jbytecode](#):
>
> [Serialization](https://docs.julialang.org/en/v1/stdlib/Serialization/) would help?

I looked at the source code of `Serialization`; it has an array of types and stuff. I don’t want to use `Serialization` because I’m writing just one parametrized type to a file, not an arbitrary data structure. I don’t want to run the risk of someone passing a file containing a data structure that could harm the program.

> [@GunnarFarneback](#):
>
> If you only want to support types in Base, the lowtech solution is to simply write the type name in the file and reconstruct it with `getfield(Base, Symbol(type_name))`.

How do I get the type name as a `String`?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [June 26, 2026, 1:31am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/7 "2026-06-26T01:31:14Z")

</div>

`Serialization` sounds like the right solution for you.

> [@phma](#):
>
> because I’m writing just one parametrized type to a file, not an arbitrary data structure.

I don’t understand why this is a blocking issue.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 26, 2026, 1:39am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/8 "2026-06-26T01:39:55Z")

</div>

> [@phma](#):
>
> How do I get the type name as a `String`?

`string` gets how a symbol or type is `print`ed, which is reliable but not a guarantee of the necessary information to parse into a type. `nameof` is the API for accessing the internal `name::Symbol` field of types, but that won’t include the parameters and can look quite different from how it’s printed e.g. `nameof(typeof([1])) == :Array` vs. `string(typeof([1])) == "Vector{Int64}".`

> [@phma](#):
>
> I don’t want to run the risk of someone passing a file containing a data structure that could harm the program.

I think commenters are making some assumptions about your package that may not be true. It could help to elaborate how you plan this file to be used e.g. when it’s generated, if different users are expected to trade files to save each other time, API on loading files, exact security concerns. The description so far suggests caching results, which may not need file management at all.

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [June 26, 2026, 4:45am UTC](https://discourse.julialang.org/t/how-to-write-a-type-to-a-file/137798/9 "2026-06-26T04:45:37Z")

</div>

I’ve succeeded in using `string` to get the name of a type, then turning it back into a type with `getfield`. It’s a bit more complicated if the type is `BFloat16`, since that’s in `Core`, not `Base`, but I can figure it out.

The files may be passed from one user to another. All programs that take input from an untrusted source (here, another user) must run security checks on the input, unless what they do is completely general (such as encrypt or try to compress the data). I’d also like the file to be small; `Serialization` being general, could make the file bigger than it needs to be. My `marshal` function turns a `Float64` into usually nine bytes; the extra byte is there in case someone makes a `HalftoneApprox` with a `BigFloat` with more than 256 bytes of precision. I’ll omit the 0th and last numbers in the `OffsetVector` because they’re always 0 and 1 respectively (and `marshal` crashes if fed a 1 or anything bigger).

I’ve posted the link to the repo on MAA, hoping someone can find a closed-form expression, but that may take years.
