Getting all types defined in a module

Hi,

Is there a way to get all the types defined in a module? For instance I am using DifferentialEquations.jl a lot and I’d like to have a list of all the types defined in that module. I feel like it would help me to have an overview of what’s going on in the code and see how good julia programmers structure their code. Also, knowing that a type exists without having to dig in the doc might help to reuse types.
Thanks

You can get a list of all the names of “things” defined in a module using the names function. It is then just a matter of filtering the ones you want based on their types. Since types themselves are of type DataType, this should do the trick:

function print_types(modul)
    for name in names(modul)
        getfield(modul, name) isa DataType && println(name)
    end
end

For example for module Pkg, we get

julia> using Pkg

julia> print_types(Pkg)
PackageMode
RegistrySpec
UpgradeLevel

(That being said, I’m not entirely sure that this will help you better understand the API of a module. Perhaps either the documentation or existing examples would be better to learn how to use DiffEq…)

2 Likes

Thanks, that helps. The function print_types() can be buggy on some modules (e.g. gives me an UndefVarError for DifferentialEquations) but it pointed me toward names() which gives me what I wanted.

I’m not really sure why there are errors of this kind, but the following should help:

function print_types(modul)
    for name in names(modul)
        try
            getfield(modul, name) isa DataType && println(name)
        catch
        end
    end
end
julia> using DifferentialEquations

julia> print_types(DifferentialEquations)
AB3
AB4
AB5
ABM32
ABM43
ABM54
AN5
...

You can export names that are not defined in a module. They are still listed in names.

julia> module Demo
       export undefined
       end
Main.Demo

julia> names(Demo)
2-element Array{Symbol,1}:
 :Demo
 :undefined
2 Likes