# Getting all types defined in a module

**URL:** <https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206>\
**Category:** New to Julia\
**Created:** [March 19, 2020, 5:42pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206 "2020-03-19T17:42:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bastien](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bastien](https://discourse.julialang.org/u/Bastien)\
**Post date:** [March 19, 2020, 5:42pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/1 "2020-03-19T17:42:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 19, 2020, 9:03pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/2 "2020-03-19T21:03:27Z")

</div>

You can get a list of all the names of “things” defined in a module using the [`names`](https://docs.julialang.org/en/v1/base/base/#Base.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:

```julia
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
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…)

---

<div class="post-metadata">

**Author:** ![Bastien](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@Bastien](https://discourse.julialang.org/u/Bastien)\
**Post date:** [March 19, 2020, 9:41pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/3 "2020-03-19T21:41:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [March 19, 2020, 10:05pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/4 "2020-03-19T22:05:44Z")

</div>

> [@Bastien](#):
>
> The function print\_types() can be buggy on some modules

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

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

```

```julia
julia> using DifferentialEquations

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

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [March 19, 2020, 10:09pm UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/5 "2020-03-19T22:09:01Z")

</div>

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

```julia
julia> module Demo
       export undefined
       end
Main.Demo

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

```

---

<div class="post-metadata">

**Author:** ![fer2tdt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fer2tdt/32/47427_2.png) [@fer2tdt](https://discourse.julialang.org/u/fer2tdt)\
**Post date:** [June 14, 2023, 7:30am UTC](https://discourse.julialang.org/t/getting-all-types-defined-in-a-module/36206/6 "2023-06-14T07:30:39Z")

</div>

Hi, the next could be better:

```julia
function print_alltypes(modul::Module)
### All objects and its types into a Module
    for name in names(modul)
        objeto=getfield(modul,name)
        tupla=supertypes(typeof(objeto))
        println("Objet ", objeto, "---", "type ",tupla[2])
    end
end

```
