I have a parametric type. For example:
> Array([1 2;3 4])
Its type is
> typeof(Array([1 2;3 4]))
Array{Int64,2}
I can get the first type parameter using eltype
:
> eltype(typeof(Array([1 2;3 4])))
Int64
How can I access second and possibly other type parameters?
If you're speaking specifically of (Abstract)Array
types, then the dimension can be retrieved using ndims
:
julia> ndims(Array{Int64, 2})
2
If, on the other hand, you want to write a custom function that extracts a given parameter from a parametric type, you can use build one like this:
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
(I'm using Array
here for the sake of the example, but the same kind of construct could be used to extract parameters from any other parametric type)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With