如何在Julia Base中查找抽象类型的子类型?
例如,假设我为如何在Julia Base中查找抽象类型的子类型?,julia,Julia,例如,假设我为AbstractFloat编写了一个函数,我想知道此方法定义的影响,如何在Base中检查AbstractFloat的所有子类型?您正在寻找的: 返回数据类型T的直接子类型列表。请注意,所有当前加载的子类型都包含在内,包括当前模块中不可见的子类型 但有趣的是,在Base中只有一个: julia> subtypes(Base, AbstractFloat) 1-element Array{Union{DataType, UnionAll},1}: BigFloat 顺便说一句
AbstractFloat
编写了一个函数,我想知道此方法定义的影响,如何在Base中检查AbstractFloat
的所有子类型?您正在寻找的:
返回数据类型T的直接子类型列表。请注意,所有当前加载的子类型都包含在内,包括当前模块中不可见的子类型
但有趣的是,在Base
中只有一个:
julia> subtypes(Base, AbstractFloat)
1-element Array{Union{DataType, UnionAll},1}:
BigFloat
顺便说一句,中有一个可视化类型树的好方法:
您可以使用typeof和methodswith来帮助将来找到类似问题的答案:
julia> methodswith(typeof(AbstractFloat))
13-element Array{Method,1}:
deserialize(s::AbstractSerializer, t::DataType) in Base.Serializer at serialize.jl:1045
dump(io::IO, x::DataType) in Base at show.jl:1304
dump(io::IO, x::DataType, n::Int64, indent) in Base at show.jl:1209
eltype(t::DataType) in Base at array.jl:46
fieldname(t::DataType, i::Integer) in Base at reflection.jl:120
fieldnames(t::DataType) in Base at reflection.jl:143
fieldoffset(x::DataType, idx::Integer) in Base at reflection.jl:335
isbits(t::DataType) in Base at reflection.jl:233
serialize(s::AbstractSerializer, t::DataType) in Base.Serializer at serialize.jl:538
show(io::IO, x::DataType) in Base at show.jl:211
subtypes(m::Module, x::Union{DataType, UnionAll}) in Base at reflection.jl:437
subtypes(x::Union{DataType, UnionAll}) in Base at reflection.jl:460
supertype(T::DataType) in Base at operators.jl:41
我是pythonista,所以我在~/.juliarc.jl
中添加了macro dir(a):(methodswith(typeof($a)))end
,现在我可以写:
julia> @dir AbstractFloat
你刚才说的答案是“AbstractFloat的子类型”:
子类型(AbstractFloat)
非常感谢。我为不知道这一点感到羞愧。你介意把它作为一个答案吗?其余的都是Core
no?@AlexanderMorley是的宏对于任何数据类型都会返回相同的结果,我猜你的意思是“methodswith(特定类型)”或“methodswith(特定实例的类型)),看起来像是一个打字错误?@gnimu是的,我计划将它用于实例。例如@dir”“
。宏不是很聪明,但我经常使用带有(typeof(something))的方法。也许它会改变,因为我还是新手。例如,这是非常无用的(在这个版本的宏中)->@dir[1.0]
。这是因为methodswith(Array{Float64,1})
返回0元素数组{Method,1}
!顺便说一句,methodswith(Array{Float64,1},true)返回1078元素数组!我可能需要完全重写它(脑海中浮现出某种数据帧:)。
julia> @dir AbstractFloat