gpt4 book ai didi

types - julia 获取类型参数的值

转载 作者:行者123 更新时间:2023-12-03 20:25:45 25 4
gpt4 key购买 nike

我有一个参数类型。例如:

> Array([1 2;3 4])

它的类型是
> typeof(Array([1 2;3 4]))

Array{Int64,2}

我可以使用 eltype 获取第一个类型参数:
> eltype(typeof(Array([1 2;3 4])))

Int64

如何访问第二个和可能的其他类型参数?

最佳答案

如果你是专门说 (Abstract)Array类型,然后可以使用 ndims 检索维度:

julia> ndims(Array{Int64, 2})
2

另一方面,如果您想编写一个从参数类型中提取给定参数的自定义函数,您可以像这样使用 build one:
julia> second_param(::Type{Array{T, N}}) where {T, N} = N
second_param (generic function with 1 method)

julia> second_param(Array{Int64, 2})
2

(为了示例,我在这里使用 Array,但可以使用相同类型的构造从任何其他参数类型中提取参数)

关于types - julia 获取类型参数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61602026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com