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

supertypes(T) function? #34362

Closed
stevengj opened this issue Jan 13, 2020 · 3 comments · Fixed by #34419
Closed

supertypes(T) function? #34362

stevengj opened this issue Jan 13, 2020 · 3 comments · Fixed by #34419
Labels
types and dispatch Types, subtyping and method dispatch

Comments

@stevengj
Copy link
Member

stevengj commented Jan 13, 2020

When trying to understand (or explain) a type T, I often find myself typing supertype(T), supertype(supertype(T)), supertype(supertype(supertype(T))), etcetera in order to see the type hierarchy. This is tedious and annoying. It would be nice to have a function supertypes(T) that just returned a tuple of T and all its supertypes, e.g.

julia> supertypes(Int)
(Int64, Signed, Integer, Real, Number, Any)

Sample implementation:

function supertypes(T::Type)
    S = supertype(T)
    return S === T ? (T,) : (T, supertypes(S)...)
end

It could be made to execute completely statically (e.g. is adding @pure enough?).

@stevengj stevengj added the types and dispatch Types, subtyping and method dispatch label Jan 13, 2020
@JeffBezanson
Copy link
Member

Sounds fine to me. I think it could return a Vector{Any} like subtypes; is there a real use case for evaluating it at compile time?

@StefanKarpinski
Copy link
Member

What about just adding supertype(T, n::Integer=1) to get the nth supertype? Perhaps both.

@stevengj
Copy link
Member Author

stevengj commented Jan 13, 2020

I guess my feeling is that since it can be static, it should be static (or at least return a tuple type that could be computed statically in a future version), since we can’t change our minds and make it static later if we return an array.

In contrast, the subtypes function cannot be static.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants