The problem with this approach:
julia> function foo(mt::MyType)
T = mt.parameters[1]
a = Array(T, 5)
a[3] = 7
a
end
foo (generic function with 1 method)
julia> @code_warntype foo(MyType{Int,Float64}())
Variables:
mt::MyType{Int64,Float64}
T::Union{}
a::Union{}
Body:
begin # none, line 2:
T = (Main.getindex)((top(getfield))
(mt::MyType{Int64,Float64},:parameters)::Union{},1)::Union{} # none, line 3:
a = call(Main.Array,T::Union{},5)::Union{} # none, line 4:
(Main.setindex!)(a::Union{},7,3)::Union{} # none, line 5:
return a::Union{}
end::Union{}
Those Union{}s will kill performance.
--Tim
On Friday, September 11, 2015 12:54:12 PM David Gold wrote:
> I'm not convinced it's more Julian to use such a helper function, since it
> will needlessly compile a different method for each distinct set of
> parameters. Directly accessing the `parameter` field of the type in
> question avoids this.
>
> On Friday, September 11, 2015 at 12:30:16 PM UTC-7, Josh Langsfeld wrote:
> > You can access the 'parameters' field of a type instance object. But the
> > standard Julian way to get type parameters is to just define a helper
> > function:
> >
> > typeparams{A,B}(::Type{T{A,B}}) = A,B
> >
> > On Friday, September 11, 2015 at 3:20:17 PM UTC-4, Erik Schnetter wrote:
> >> Is there a function in Julia that allows accessing the parameters of a
> >> type?
> >>
> >> For example, if I have
> >>
> >> type T{A,B} end
> >>
> >> then I'd like a way to convert `T{Int, Char}` to `(Int, Char)`.
> >>
> >> In other words, is there a way to get at the contents of `DataType`
> >> objects?
> >>
> >> Thanks,
> >> -erik