In C# and Java, we have the toString() method to return a String representation of an object.
The closest I've seen in Julia is show(), but I'm not entirely sure if it's the correct function.
struct Person
name::String
age::Int32
end
amber = Person("Amber",22)
println(amber) # Output: Person("Amber", 22)
If I want a little more control over the return String, I can do:
function Base.show(io::IO, person::Person)
return "Name: $(person.name) Age: $(person.age)"
end
println(show(amber)) # Output: Name: Amber Age: 22
Do I have to call show() directly everytime? Can I pass it to println() and have it call show()? In C# and Java, if toString() is overridden, Console.writeLine() or System.out.println() will call toString(), no need to call it directly.