DEV Community

Cover image for Learning Julia (7): apropos Types
Z. QIU
Z. QIU

Posted on

Learning Julia (7): apropos Types

Everything in Julia has its type, we can use typeof() to obtain its type. There is a complex hierarchical type Tree structure in Julia, meaning that every non-root type has its 'parent' type from which it is 'derived'. We can use supertype() to get parent type and subtypes() to get all its children types:

Alt Text

I wrote several lines of code for studying this tree-structure of Types in Julia. The root variable is defined as input which should be a Type in Julia and this file shall output all its derived types. The output of this code is a file where each line contains a type-type pair and their relation (simple entity-relation case of Knowledge Graph).


using InteractiveUtils   # thus we can call subtypes() without interactive mode

root = Real    # or other types that we want to get all its derivative types

## a  parent - (relation) -> son data struct
struct TypeLink
    node_parent::String
    relation::String
    node_son::String
end


function getLink(parentName, childName)
    return TypeLink(parentName, "is_parent_of", childName)
end

l = TypeLink[]


println(length(subtypes(root)))
function getAllChildren(node)
    if length(subtypes(node)) > 0
        for i in subtypes(node)
            s = getLink(string(node), string(i))
            push!(l, s)
            # println( string(i))
            if i != Any
                getAllChildren(i)
            end
        end
    end
    return
end


# GO! 
getAllChildren(root)
println(length(l))

# save to file
open(string(root)*"Type.txt", "w") do f
    for i in l
        n1 = i.node_parent
        n2 = i.node_son
        n3 = i.relation
        write(f, "$n1, $n2, $n3 \n")
    end
end
Enter fullscreen mode Exit fullscreen mode

The output file content is shown below:
Alt Text

Now I load the output data to Neo4j, and check the output:
Alt Text

I tried also obtaining all derived type of Any type. This yields more than 10k links:

Alt Text

Impossible to plot the entire graph, I show a partial graph who contains 100 relations:
Alt Text

Top comments (0)