From f2b876a0803699441ba6ce30ce80d854a9118d1a Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 30 Jan 2020 14:31:09 -0500 Subject: [PATCH] add supertypes(T) function (#34419) --- NEWS.md | 1 + stdlib/InteractiveUtils/docs/src/index.md | 1 + .../InteractiveUtils/src/InteractiveUtils.jl | 22 ++++++++++++++++++- stdlib/InteractiveUtils/test/runtests.jl | 6 +++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index b2f473e305232a..045cb71e02149c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -59,6 +59,7 @@ New library features Standard library changes ------------------------ * The `@timed` macro now returns a `NamedTuple` ([#34149]) +* New `supertypes(T)` function returns a tuple of all supertypes of `T` ([#34419]). #### LinearAlgebra * The BLAS submodule now supports the level-2 BLAS subroutine `hpmv!` ([#34211]). diff --git a/stdlib/InteractiveUtils/docs/src/index.md b/stdlib/InteractiveUtils/docs/src/index.md index 9c866c286e0796..a8a675e207a18b 100644 --- a/stdlib/InteractiveUtils/docs/src/index.md +++ b/stdlib/InteractiveUtils/docs/src/index.md @@ -6,6 +6,7 @@ InteractiveUtils.varinfo InteractiveUtils.versioninfo InteractiveUtils.methodswith InteractiveUtils.subtypes +InteractiveUtils.supertypes InteractiveUtils.edit(::AbstractString, ::Integer) InteractiveUtils.edit(::Any) InteractiveUtils.@edit diff --git a/stdlib/InteractiveUtils/src/InteractiveUtils.jl b/stdlib/InteractiveUtils/src/InteractiveUtils.jl index 239e138c7fa038..367889d029fe24 100644 --- a/stdlib/InteractiveUtils/src/InteractiveUtils.jl +++ b/stdlib/InteractiveUtils/src/InteractiveUtils.jl @@ -3,7 +3,7 @@ module InteractiveUtils export apropos, edit, less, code_warntype, code_llvm, code_native, methodswith, varinfo, - versioninfo, subtypes, @which, @edit, @less, @functionloc, @code_warntype, + versioninfo, subtypes, supertypes, @which, @edit, @less, @functionloc, @code_warntype, @code_typed, @code_lowered, @code_llvm, @code_native, clipboard import Base.Docs.apropos @@ -239,6 +239,26 @@ julia> subtypes(Integer) """ subtypes(x::Type) = _subtypes_in(Base.loaded_modules_array(), x) +""" + supertypes(T::Type) + +Return a tuple `(T, ..., Any)` of `T` and all its supertypes, as determined by +successive calls to the the [`supertype`](@ref) function, listed in order of `<:` +and terminated by `Any`. + +# Examples +```jldoctest +julia> supertypes(Int) +(Int64, Signed, Integer, Real, Number, Any) +``` +""" +function supertypes(T::Type) + S = supertype(T) + # note: we return a tuple here, not an Array as for subtypes, because in + # the future we could evaluate this function statically if desired. + return S === T ? (T,) : (T, supertypes(S)...) +end + # dumptype is for displaying abstract type hierarchies, # based on Jameson Nash's typetree.jl in https://github.com/JuliaArchive/Examples function dumptype(io::IO, @nospecialize(x), n::Int, indent) diff --git a/stdlib/InteractiveUtils/test/runtests.jl b/stdlib/InteractiveUtils/test/runtests.jl index 42a4af13a9a816..0cbd98bcc7b582 100644 --- a/stdlib/InteractiveUtils/test/runtests.jl +++ b/stdlib/InteractiveUtils/test/runtests.jl @@ -23,6 +23,12 @@ struct B20086{T,N} <: A20086{T,N} end @test subtypes(A20086{T,3} where T) == [B20086{T,3} where T] @test subtypes(A20086{Int,3}) == [B20086{Int,3}] +# supertypes +@test supertypes(B20086) == (B20086, A20086, Any) +@test supertypes(B20086{Int}) == (B20086{Int}, A20086{Int}, Any) +@test supertypes(B20086{Int,2}) == (B20086{Int,2}, A20086{Int,2}, Any) +@test supertypes(Any) == (Any,) + # code_warntype module WarnType using Test, Random, InteractiveUtils