> I have an instance of a parametric type, is there a canonical way to get
> the parameter values?
> For example I could have some Array{T, d} and want to know whether T is
> Float64 and what its dimension is.
> The only way of doing this I am aware of is writing code like:
>
> dimension{T, dim}(arr::Array{T, dim}) = dim
> numbertype{T, dim}(arr::Array{T, dim}) = T
>
> Is there a built-in function to do this? And maybe a way to get parameter
> values for arbitrary types?
> E.g. I have some instance of MyType{S, T, U, n} is and want to know U. Do I
> have to repeat code like the above each time I define a new parametric type?

Yes, this is the correct and Julian way to do this.  One of the reason
why there is no built-in function for this is that it doesn't make much
sense to ask for the, say, second parameter of an arbitrary type, as
that does not tell you much: you need to know the type to make sense of
the parameters.  However, if you need to do it generically, then Bart's
solution is the way to go.  Note however that type inference fails on
it, thus it shouldn't be used in performance critical code-paths:

julia> dimension{T, dim}(arr::Array{T, dim}) = dim
dimension (generic function with 1 method)

julia> @code_warntype dimension(rand(2))
Variables:
  arr::Array{Float64,1}

Body:
  begin  # none, line 1:
      return 1
  end::Int64

julia> dimension2{T}(arr::T) = T.parameters[2]
dimension2 (generic function with 1 method)

julia> @code_warntype dimension2(rand(2))
Variables:
  arr::Array{Float64,1}

Body:
  begin  # none, line 1:
      return 
(Main.getindex)((top(getfield))(T,:parameters)::SimpleVector,2)::Any
  end::Any

Reply via email to