# Custom printing of struct getting printed before string

**URL:** <https://discourse.julialang.org/t/custom-printing-of-struct-getting-printed-before-string/95024>\
**Category:** General Usage\
**Created:** [February 22, 2023, 2:51pm UTC](https://discourse.julialang.org/t/custom-printing-of-struct-getting-printed-before-string/95024 "2023-02-22T14:51:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hshackle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hshackle/32/47114_2.png) [@hshackle](https://discourse.julialang.org/u/hshackle)\
**Post date:** [February 22, 2023, 2:51pm UTC](https://discourse.julialang.org/t/custom-printing-of-struct-getting-printed-before-string/95024/1 "2023-02-22T14:51:32Z")

</div>

I have a struct that I would like to define a custom way of printing it. This is done by overloading the `Base.show` function:

```julia
struct customType
    a::Int
    b::Int
end

function Base.show(io::IO, state::customType)
    print("$(state.a), $(state.b)")
end

```

This works fine if I just call `println(A)` . However, if I call it in the context of a string, i.e. `println("Custom struct is: $(A)")`, the printing of `A` happens _before_ the rest of the string. i.e the output of the code

```julia
A = customType(1, 2)
println(A)
println("custom type is $(A)")

```

is

```julia
1, 2
1, 2custom type is

```

How do I fix this?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [February 22, 2023, 2:55pm UTC](https://discourse.julialang.org/t/custom-printing-of-struct-getting-printed-before-string/95024/2 "2023-02-22T14:55:58Z")

</div>

You must pass `io` to `print` in your definition of `show`.

---

<div class="post-metadata">

**Author:** ![hshackle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hshackle/32/47114_2.png) [@hshackle](https://discourse.julialang.org/u/hshackle)\
**Post date:** [February 22, 2023, 2:57pm UTC](https://discourse.julialang.org/t/custom-printing-of-struct-getting-printed-before-string/95024/3 "2023-02-22T14:57:53Z")

</div>

Got it, thanks!
