Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualize the Julia Type Tree

Tags:

julia

Is there a convenient way to visualize the Julia type tree? I know I can write a function for that...

function ttree(t::Type, indent = "    ")
    println(string(indent, t))
    indent *= "    "
    for st in subtypes(t)
        ttree(st, indent)
    end
end

ttree(Integer)

    Integer
        Bool
        Signed
            BigInt
            Int128
            Int16
            Int32
            Int64
            Int8
        Unsigned
            UInt128
            UInt16
            UInt32
            UInt64
            UInt8

...but Julia's strong pronunciation of multiple dispatch makes me feel like there must be some cool built-in function for that, right?

like image 974
Georgery Avatar asked Sep 06 '25 03:09

Georgery


2 Answers

Use GraphRecipes:

using GraphRecipes, Plots
plot(Integer, method=:tree, fontsize=10, nodeshape=:rect)

enter image description here

Here is an ASCII approach

using AbstractTrees
AbstractTrees.children(d::DataType) = subtypes(d)

And here it is in action

julia> print_tree(Integer)
Integer
├─ Bool
├─ Signed
│  ├─ BigInt
│  ├─ Int128
│  ├─ Int16
│  ├─ Int32
│  ├─ Int64
│  └─ Int8
└─ Unsigned
   ├─ UInt128
   ├─ UInt16
   ├─ UInt32
   ├─ UInt64
   └─ UInt8
like image 74
Przemyslaw Szufel Avatar answered Sep 07 '25 23:09

Przemyslaw Szufel


Term.jl provides a typestree function that produces a beautiful tree of types:

Term.typestree output, a coloured tree of types

But it only seems to go upwards i.e. the supertype hierarchy of a given type. An option to dig down to get lower level subtypes doesn't (as of Term v1.0.4) seem to exist. It does print out the immediate subtypes of a type, just not the levels down from those. typestree output for Integer type

like image 36
Sundar R Avatar answered Sep 07 '25 22:09

Sundar R