Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typetrace(t) for returning the recursive supertypes of Type t #34718

Closed
wants to merge 8 commits into from
28 changes: 28 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ function supertype(T::UnionAll)
UnionAll(T.var, supertype(T.body))
end

"""
typetrace(T::DataType; reeturnarray=false)

Return the recursive supertypes of DataType `T` until Any.

# Examples
```jldoctest
julia> typetrace(Int32)
"Int32 <: Signed <: Integer <: Real <: Number <: Any"

julia> typetrace(Int32, returnarray=true)
6-element Array{Type,1}:
Any
Number
Real
Integer
Signed
Int32
```
"""
function typetrace(t::DataType; returnarray=false)
if returnarray
push!(t == Any ? DataType[] : typetrace(supertype(t), returnarray=true)::Vector{DataType}, t)
else
"$t$(t == Any ? "" : " <: $(typetrace(supertype(t)))")"
end
end

## generic comparison ##

"""
Expand Down