Consider the output of subtypes
function:
julia> subtypes(Associative)
5-element Array{Union{DataType, UnionAll},1}:
Base.EnvHash
Base.ImmutableDict
Dict
ObjectIdDict
WeakKeyDict
What is the reason of having some types with Base-prefix, and others – without? Can I control this somehow? I use subtype
to traverse type hierarchy for doing various analysis, and I would like to have consistency: either all types are prefixed with the package name or none of them are. Is it possible?
I believe it is because some of those names are exported from Base and the others are not.
This may work as you expect:
julia> function qualified_name(typ)
return string(Base.datatype_module(typ), ".", split(string(typ), ".")[end])
end
julia> qualified_name(Int)
"Core.Int64"
julia> qualified_name(Dict)
"Base.Dict{K,V}"
julia> qualified_name(Base.ImmutableDict)
"Base.ImmutableDict{K,V}"
Here I use https://docs.julialang.org/en/stable/stdlib/base/?highlight=reflection#Base.Base.datatype_module.
1 Like
@bicycle1885 this is not working with Julia 0.6-rc2
ERROR: MethodError: no method matching datatype_module(::Type{Dict})
Closest candidates are:
datatype_module(::DataType) at reflection.jl:160
Did you try it? Which Julia version?
UPD: this worked for me:
get_datatype(t :: DataType) = t
get_datatype(t :: UnionAll) = Base.unwrap_unionall(t)
function qualified_name(t)
base = split("$(t)", ".")[end]
"$(get_datatype(t).name.module).$(base)"
end