Tuple type to tuple of types

91 views
Skip to first unread message

jw3126

unread,
Jul 24, 2016, 7:52:47 AM7/24/16
to julia-users
I need a function, which accepts an arbitrary tuple type and returns the types of the components of the tuple. For example
```
f(Tuple{Int64, Float64})                --> (Int64, Float64)
f(Tuple{Int64, MyType, Float32}) --> (Int64, MyType, Float32)
f(NTuple{3})                                  --> (Any, Any, Any)
f(Tuple)                                         --> Some error since length is unknown
```

How to accomplish this?
Message has been deleted

David P. Sanders

unread,
Jul 24, 2016, 10:59:05 AM7/24/16
to julia-users
This information is stored inside the tuple type:

julia> t = Tuple{Int64, Float64}
Tuple{Int64,Float64}

julia> t.parameters
svec(Int64,Float64)

julia> t.parameters[1]
Int64
 

How to accomplish this?

Tim Holy

unread,
Jul 24, 2016, 11:05:16 AM7/24/16
to julia...@googlegroups.com
As long as you don't mind preserving exactly what's between the {},
(t.parameters...)
is an easy way to get this.

--Tim

On Sunday, July 24, 2016 5:21:56 AM CDT Kristoffer Carlsson wrote:
> Maybe;
>
> type MyType end
>
> function f(t::DataType)
> I = ()
> for t in t.types
> if isa(t, TypeVar)
> I = (I..., Any)
> else
> I = (I..., t)
> end
> end
> return I
> end
>
> julia> f(Tuple{Int64, Float64})
> (Int64,Float64)
>
> julia> f(Tuple{Int64, MyType, Float32})
> (Int64,MyType,Float32)
>
> julia> f(NTuple{3})
> (Any,Any,Any)
>
> julia> f(Tuple)
> (Vararg{Any,N},julia> f(Tuple{Int64, Float64})
> (Int64,Float64)
>
> Probably has crappy performance though.

jw3126

unread,
Jul 26, 2016, 5:05:29 AM7/26/16
to julia-users
Thanks!


On Sunday, July 24, 2016 at 1:52:47 PM UTC+2, jw3126 wrote:
Reply all
Reply to author
Forward
0 new messages