# How to use \`TOML.print\` to print a \`Dict\` with keys as different \`MyStruct\` objects?

**URL:** https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804
**Category:** General Usage
**Tags:** toml, dict
**Created:** [June 25, 2023, 6:22am UTC](https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804 "2023-06-25T06:22:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 25, 2023, 6:22am UTC](https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804/1 "2023-06-25T06:22:22Z")

</div>

I’m currently working with a dictionary whose keys are instances of a custom struct named `MyStruct`. Here is the definition:

```julia
struct MyStruct
    a::Int
    b::String
end

```

I need to use the `TOML.print` function to print this dictionary in a TOML-compatible format. When I print a `Dict` where `MyStruct` is the value, like the example below, everything works fine:

```julia
TOML.print(Dict("foo" => MyStruct(5, "bar"))) do x
    x isa MyStruct && return [x.a, x.b]
    error("unhandled type $(typeof(x))")
end

```

However, I’m struggling to adapt this approach for a dictionary where `MyStruct` instances are used as keys. I understand that TOML doesn’t natively support composite types like my custom struct as keys, so I’m looking for a workaround or a best practice to follow.

```julia
dict = Dict(MyStruct(1, "one") => "value1", MyStruct(2, "two") => "value2")

```

```julia-repl
julia> dict = Dict(MyStruct(1, "one") => "value1", MyStruct(2, "two") => "value2")
Dict{MyStruct, String} with 2 entries:
  MyStruct(2, "two") => "value2"
  MyStruct(1, "one") => "value1"

julia> TOML.print(dict) do x
           x isa MyStruct && return Dict("x" => x.x, "y" => x.y)
       end
ERROR: MethodError: no method matching String(::MyStruct)

Closest candidates are:
  String(::String)
   @ Core boot.jl:360
  String(::Core.Compiler.LazyString)
   @ Core strings/lazy.jl:80
  String(::LazyString)
   @ Base strings/lazy.jl:80
  ...

Stacktrace:
 [1] print_table(f::var"#7#8", io::Base.TTY, a::Dict{MyStruct, String}, ks::Vector{String}; indent::Int64, first_block::Bool, sorted::Bool, by::Function)
   @ TOML.Internals.Printer ~/.asdf/installs/julia/1.9.1/share/julia/stdlib/v1.9/TOML/src/print.jl:152
 [2] print_table (repeats 2 times)
   @ ~/.asdf/installs/julia/1.9.1/share/julia/stdlib/v1.9/TOML/src/print.jl:131 [inlined]
 [3] #print#6
   @ ~/.asdf/installs/julia/1.9.1/share/julia/stdlib/v1.9/TOML/src/print.jl:204 [inlined]
 [4] print(f::var"#7#8", a::Dict{MyStruct, String}; sorted::Bool, by::Function)
   @ TOML.Internals.Printer ~/.asdf/installs/julia/1.9.1/share/julia/stdlib/v1.9/TOML/src/print.jl:205
 [5] print(f::var"#7#8", a::Dict{MyStruct, String})
   @ TOML.Internals.Printer ~/.asdf/installs/julia/1.9.1/share/julia/stdlib/v1.9/TOML/src/print.jl:205
 [6] top-level scope
   @ REPL[6]:1

```

Does anyone have suggestions on how I might achieve this? Any help would be appreciated!

---

<div class="post-metadata">

### Author: ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)
#### Post date: [June 25, 2023, 4:31pm UTC](https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804/2 "2023-06-25T16:31:29Z")

</div>

I’m not at a computer right now so can’t type up a complete answer. But you can do this pretty easily by passing a function as the first argument to TOML print (see docstring using ? In the repl). You could then use e.g. propertynames and getproperty inside that function to generate a Dict.

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 25, 2023, 8:47pm UTC](https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804/3 "2023-06-25T20:47:59Z")

</div>

Do you mean this example? It’s from the documentation:

```julia
TOML.print(Dict("foo" => MyStruct(5, "bar"))) do x
    x isa MyStruct && return [x.a, x.b]
    error("unhandled type $(typeof(x))")
end

```

However, it’s for printing `MyStruct` as values, not keys.

---

<div class="post-metadata">

### Author: ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)
#### Post date: [June 25, 2023, 10:41pm UTC](https://discourse.julialang.org/t/how-to-use-toml-print-to-print-a-dict-with-keys-as-different-mystruct-objects/100804/4 "2023-06-25T22:41:49Z")

</div>

OK, using a `Dict`s as keys in a `TOML` file seems to be illegal syntax, which marks this question meaningless.
