如何在 Julia 中获取 typeof 函数属性

How to get typeof function properties in Julia

如何在 Julia

中获取 typeof 属性
a = reshape(1:15,3,5)

b = typeof(a)
Base.ReshapedArray{Int64, 2, UnitRange{Int64}, Tuple{}}

如何获得 'Int64' 属性 的 'b'?

'2'是维数。正确的。如何从 'b' 获取该信息?

有函数ndims获取数组的维数,eltype用于return类型:

julia> a = reshape(1:15,3,5);

julia> ndims(a)
2

julia> eltype(a)
Int64

更一般地获取类型参数,您可以使用多个分派:

julia> my_eltype(x::AbstractArray{T}) where T = T
my_eltype (generic function with 1 method)

julia> my_eltype(a)
Int64

julia> my_ndims(x::AbstractArray{T,N}) where {T,N} = N
my_ndims (generic function with 1 method)

julia> my_ndims(a)
2