# Confusion over string interpolation of a struct

**URL:** <https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569>\
**Category:** New to Julia\
**Created:** [December 12, 2022, 8:25pm UTC](https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569 "2022-12-12T20:25:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [December 12, 2022, 8:25pm UTC](https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569/1 "2022-12-12T20:25:57Z")

</div>

I would like to write a summary of a struct as a [`string`](https://docs.julialang.org/en/v1/base/strings/#Base.string).

So I did the following:

```julia
julia> struct Cell{R<:Int}
           row::R
           col::R
       end

julia> import Base.print

julia> print(io::IO, c::Cell) = print(">>($(c.row),$(c.col))")
print (generic function with 32 methods)

julia> c=Cell(2,3)
Cell{Int64}(2, 3)

julia> "it is $c currently"
>>(2,3)"it is currently"

```

I think it is clear that this is not useful output.

My inclination would have been to write a `string()` method but if I understand the [docs](https://docs.julialang.org/en/v1/manual/strings/#string-interpolation) correctly `print()` is the preferred method to overload:

“Both concatenation and string interpolation call [`string`](https://docs.julialang.org/en/v1/base/strings/#Base.string) to convert objects into string form. However, `string` actually just returns the output of [`print`](https://docs.julialang.org/en/v1/base/io-network/#Base.print), so new types should add methods to [`print`](https://docs.julialang.org/en/v1/base/io-network/#Base.print) or [`show`]([I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.show-Tuple){IO, Any}) instead of `string`.”

Could somebody point me in teh right direction, please?

Many thanks.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [December 12, 2022, 8:38pm UTC](https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569/2 "2022-12-12T20:38:47Z")

</div>

Note that your `print` function does not use the `io` argument, i.e., always prints to stdout. In the string interpolation `print` gets passed an `IOBuffer` instead and should write to that.  
Long story short, your example works for me if I define:

```julia
print(io::IO, c::Cell) = print(io, ">>($(c.row),$(c.col))")

```

---

<div class="post-metadata">

**Author:** ![healyp](https://avatars.discourse-cdn.com/v4/letter/h/67e7ee/32.png) [@healyp](https://discourse.julialang.org/u/healyp)\
**Post date:** [December 12, 2022, 8:42pm UTC](https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569/3 "2022-12-12T20:42:03Z")

</div>

Doh. Thank you. Time to shut down for the day 🙂

---

<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:** [December 12, 2022, 9:10pm UTC](https://discourse.julialang.org/t/confusion-over-string-interpolation-of-a-struct/91569/4 "2022-12-12T21:10:38Z")

</div>

Have a look at the docstring for `show`. I believe you should overload show rather than print.
