Somewhat academical questions: Would it be possible to generalise
firstparam and secondparam to any type and number of parameters?
Something like
param(SomeType, n) # -> returns nth parameter
To generalise to any type, I think something like this would be needed:
firstparam{T,A}(::Type{T{A}}) = A
which doesn't exist (yet). Then, to generalise to any number of
parameters, would there be a better way than:
function param{T}(::Type{T}, i)
if i==1
return firstparam(T)
elseif i==2
return secondparam(T)
else
error("not implemented")
end
end
?
Presumably for now we'd be stuck with:
param{T}(::Type{T}, i) = T.parameters[i]
On Sat, 2015-09-12 at 12:43, Tim Holy <[email protected]> wrote:
> All I mean is that if you write functions that call `firstparam` or
> `secondparam`, julia will be able to infer the return type. That means that in
>
> function foo(mt::MyType)
> T = firstparam(typeof(mt))
> a = Array(T,20)
> # now do something with a
> end
>
> julia will generate efficient code in manipulating a. With the version that
> uses
> the parameters field directly, it won't.
>
> In addition to the two methods I showed in the last email, you should also
> define
>
> firstparam(mt::MyType) = firstparam(typeof(mt))
> secondparam(mt::MyType) = secondparam(typeof(mt))
>
> and that will make the code above a little nicer.
>
> Best,
> --Tim
>
> On Saturday, September 12, 2015 03:27:20 AM Jeffrey Sarnoff wrote:
>> pls show me a quick example of using that inferrably
>>
>> On Saturday, September 12, 2015 at 6:12:21 AM UTC-4, Tim Holy wrote:
>> > A much better way (because it's inferrable):
>> >
>> > firstparam{A,B}(::Type{MyType{A,B}}) = A
>> > secondparam{A,B}(::Type{MyType{A,B}}) = B
>> >
>> > --Tim
>> >
>> > On Friday, September 11, 2015 06:40:59 PM Jeffrey Sarnoff wrote:
>> > > julia> type MyType{A,B} end
>> > >
>> > > julia> ((MyType.parameters)...)
>> > > (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