kungfooman
kungfooman

Reputation: 4893

Get all implementations of an abstract type

Is there any way to get all implementations of some abstract type? Like:

implementations(AbstractString) == [String, DirectIndexString, ...]

Would be really handy. Currently I just register all implementations by hand when I need this functionality.

Upvotes: 2

Views: 186

Answers (1)

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22225

I think this is what you mean

julia> subtypes(AbstractString)
6-element Array{Union{DataType, UnionAll},1}:
 Base.SubstitutionString
 Base.Test.GenericString
 DirectIndexString      
 RevString              
 String                 
 SubString  

Equally, the reverse of this is supertype, though, by contrast, if you want to travel up the tree you'll have to do that in steps. Then again, subtypes also just gives you subtypes one level down, and you can still use it recursively to travel down the type tree.

If what you mean instead is to find only concrete implementations that are subtypes of that type, you can go through all subtypes recursively until you reach bottom, and you can then further also use isleaftype to test if they are concrete types on top of that.

Note: A parametric type may return false with isleaftype even though it has no subtypes under it. e.g. isleaftype(SubString) returns false, but isleaftype(Substring{String}) returns true.

Upvotes: 3

Related Questions