TOML.print NamedTuple

Hi all,

I am trying to write a function that should create a .toml file. Here is a simplified version:

using TOML
data = Dict(“settings” => (a = 2, b = 3))
fname = “example.toml”
open(fname, “w”) do io
TOML.print(io, data)
end

Unfortunately I get the following error that I am not sure how to deal with:
type @NamedTuple{a::Int64, b::Int64} is not a valid TOML type, pass a conversion function to TOML.print

Thanks ever so much to everyone!

(a = 2, b = 3) is a named tuple and from the error message it is clear that TOML doesn’t support that. The easiest solution is probably to pass a Dict instead:

data = Dict(“settings” => Dict("a" => 2, "b" => 3)))

or if your data was already given as named tuple, the slightly more obscure

data = Dict(“settings” => pairs((a = 2, b = 3)))
1 Like

As the error message says, TOML.print doesn’t by default have a rule on how to write the NamedTuple (a=2, b=3).

Simplest way is to pass Dict("a"=>2, "b"=>3) instead.

Or you can give a rule for named tuples by passing a conversion function. See here: TOML · The Julia Language

1 Like