Navigating the type hierarchy

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.

The startup.jl file is a user-customized script that is located in the $HOME/.julia/config directory. It can be used to store any code or functions that the user wants to run every time the REPL is started. 

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.125.219