Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

julia get value of type parameters

Tags:

types

julia

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?

like image 739
Sergey Avatar asked Mar 03 '23 11:03

Sergey


1 Answers

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)

like image 91
François Févotte Avatar answered May 11 '23 00:05

François Févotte