# How to stringify a recursively constructed type

**URL:** https://discourse.julialang.org/t/how-to-stringify-a-recursively-constructed-type/98143
**Category:** General Usage
**Tags:** question, io
**Created:** [April 30, 2023, 7:55pm UTC](https://discourse.julialang.org/t/how-to-stringify-a-recursively-constructed-type/98143 "2023-04-30T19:55:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)
#### Post date: [April 30, 2023, 7:55pm UTC](https://discourse.julialang.org/t/how-to-stringify-a-recursively-constructed-type/98143/1 "2023-04-30T19:55:13Z")

</div>

I have defined a type representing a node in an execution tree. Sometimes it only contains a variable, sometimes it contains an operator and 1 or 2 arguments. Now I want to display it as a string by recursing down the tree, and I’m not sure what I should be overloading to achieve this.

The documentation seems to be saying that `string()` calls `show()`, and that I should therefore overload `show()`, but `show()` also contains the argument `io`, and I don’t see how to concatenate the strings that result from the recursive calls down the tree. I assume I have misunderstood something, but I can’t find an explanation that I can understand.

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)
#### Post date: [May 1, 2023, 7:12am UTC](https://discourse.julialang.org/t/how-to-stringify-a-recursively-constructed-type/98143/2 "2023-05-01T07:12:40Z")

</div>

One you have overloaded `show`, `string` will use that automatically. You overload something like this

```julia
function Base.show(io::IO, x::YourType)
    print(io, "YourType: ")
    for child in x.children
        print(io, info_about_the(child)) # add whatever you want from your data structure
    end
end

```

---

<div class="post-metadata">

### Author: ![Niall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niall/32/22517_2.png) [@Niall](https://discourse.julialang.org/u/Niall)
#### Post date: [May 1, 2023, 6:19pm UTC](https://discourse.julialang.org/t/how-to-stringify-a-recursively-constructed-type/98143/3 "2023-05-01T18:19:31Z")

</div>

Oh right - I think I see now! print() itself makes a recursive call back to show() if info\_about\_the(child) turns out itself to be of type YourType, is that correct?

Best wishes,

Niall.
