Julia provides some convenient functions to navigate the type hierarchy. To find the subtypes of an existing type, we can use the subtypes function:
Similarly, to find the supertype of an existing type, we can use the supertype function.
Sometimes, it's convenient to see the complete hierarchy in a tree format. Julia comes with no standard function that we can use to achieve this, but we can easily create one ourselves using a recursion technique, as follows:
# Display the entire type hierarchy starting from the specified `roottype`
function subtypetree(roottype, level = 1, indent = 4)
level == 1 && println(roottype)
for s in subtypes(roottype)
println(join(fill(" ", level * indent)) * string(s))
subtypetree(s, level + 1, indent)
end
end
This function can be quite convenient for new Julia users. In fact, I have the code saved in my startup.jl file so that it is loaded into the REPL automatically.
We can now display the personal asset type hierarchy easily, as follows:
Note that this function can only display a hierarchy of types that have already been loaded into memory. Now that we have defined abstract types, we should be able to associate functions with them. Let's do that next.