How to recover the parameters of a parametric type?

Is there any function that receives a parametric type as input and returns the parameters? For example, does the function below exist?

julia> get_parameters(Array{Float64, 2})
(Float64, 2)

In case get_parameters doesn’t exist, what are the alternatives?

I am asking this question because I am writing numerical algorithms with the fixed points of the FixedPointNumbers.jl package. When my function g() receives a Fixed{T,f} number, it needs to know the parameters T and f because they will determine the accuracy of my algorithm.

How about this?

get_T(::Fixed{T, f}) where {T, f} = T

get_f(::Fixed{T, f}) where {T, f} = f

Those functions expect to be passed a value of some Fixed type. If you want to pass the type itself, you can do:

get_T(::Type{Fixed{T, f}}) where {T, f} = T

eltype() ?

It worked, thanks!

Thank you for helping me. I did not understand eltype behavior very well, I tried using it in the REPL

julia> x = Fixed{Int64, 51}(9.3)

julia> eltype(x)

But I do not know how to use it to extract Int64 and 51.

julia> x = Fixed{Int64, 51}(9.3)

julia> eltype(x)

julia> typeof(x.i)

I don’t think you can get f any easy way.

Oh, yes you can:

julia> x = Fixed{Int32, 13}(9.3)

julia> z = Base.decompose(x)
(76186, -13, 1)
T = typeof(z[1])
f = -z[2]
1 Like

Sorry, I was typing from my mobile without proper testing. eltype() seems working only for containers-like parametric types, and any-how it returns only the type parameter (e.g. eltype([1,2]) returns Int64).