How to stringify a recursively constructed type

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.

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

function, x::YourType)
    print(io, "YourType: ")
    for child in x.children
        print(io, info_about_the(child)) # add whatever you want from your data structure
1 Like

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,