Consistency
Consistency

Reputation: 2922

How to find the subtypes of an abstract type in Julia Base?

For example, suppose I write a function for AbstractFloat, I want to know the impact of this method definition, how do I check all the subtypes of AbstractFloat in Base?

Upvotes: 3

Views: 2833

Answers (3)

Alexander Rakhmaev
Alexander Rakhmaev

Reputation: 1055

Recently, this function needs to be connected from the InteractiveUtils package. See SOF page for details.

Upvotes: 0

Liso
Liso

Reputation: 2260

You could use typeof and methodswith to help find answer to similar question in future:

julia> methodswith(typeof(AbstractFloat))
13-element Array{Method,1}:
 deserialize(s::AbstractSerializer, t::DataType) in Base.Serializer at     serialize.jl:1045
 dump(io::IO, x::DataType) in Base at show.jl:1304                                                       
 dump(io::IO, x::DataType, n::Int64, indent) in Base at show.jl:1209                                     
 eltype(t::DataType) in Base at array.jl:46                                                              
 fieldname(t::DataType, i::Integer) in Base at reflection.jl:120                                         
 fieldnames(t::DataType) in Base at reflection.jl:143                                                    
 fieldoffset(x::DataType, idx::Integer) in Base at reflection.jl:335                                     
 isbits(t::DataType) in Base at reflection.jl:233                                                        
 serialize(s::AbstractSerializer, t::DataType) in Base.Serializer at serialize.jl:538                    
 show(io::IO, x::DataType) in Base at show.jl:211                                                        
 subtypes(m::Module, x::Union{DataType, UnionAll}) in Base at reflection.jl:437                          
 subtypes(x::Union{DataType, UnionAll}) in Base at reflection.jl:460                                     
 supertype(T::DataType) in Base at operators.jl:41      

I am pythonista so I added macro dir(a) :(methodswith(typeof($a))) end into ~/.juliarc.jl and now I could write:

julia> @dir AbstractFloat

Upvotes: 2

Gnimuc
Gnimuc

Reputation: 8566

you were looking for subtypes:

Return a list of immediate subtypes of DataType T. Note that all currently loaded subtypes are included, including those not visible in the current module.

julia> subtypes(AbstractFloat)
4-element Array{Union{DataType, UnionAll},1}:
 BigFloat
 Float16 
 Float32 
 Float64 

But interestingly, there is only one in Base:

julia> subtypes(Base, AbstractFloat)
1-element Array{Union{DataType, UnionAll},1}:
 BigFloat

BTW, there is a nice recipe in PlotRecipes.jl for visualizing the type tree: enter image description here

Upvotes: 8

Related Questions