I’m trying to count all subtypes of given type (subtypes of these subtypes and so on) with a recursive function, but for now I only managed to write one showing the structure by creating arrays in arrays…
function allSubtypes(T::Type)
if length(subtypes(T)) > 0
return allSubtypes.(subtypes(T))
else
return T
end
end
julia> function countsubtypes(T::Type; recursive=false)
_subtypes = subtypes(T)
n = length(_subtypes)
if !recursive
return n
end
n + sum(countsubtypes(_T) for _T in _subtypes)
end
countsubtypes (generic function with 1 method)
julia> countsubtypes(Integer; recursive=true)
14
julia> countsubtypes(Integer; recursive=false)
3
julia> countsubtypes(Signed; recursive=false)
6
julia> countsubtypes(Signed; recursive=true)
6
You have to make the call of the countsubtypes function in the sum recursive to walk the full subtype tree.
function countsubtypes(T::Type; recursive=false)
_subtypes = subtypes(T)
@show _subtypes
n = length(_subtypes)
if !recursive || n == 0
return n
end
n + sum(countsubtypes(_T, recursive=true) for _T in _subtypes)
end