# Promote\_type of nested tuple elements

**URL:** https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761
**Category:** General Usage
**Created:** [March 19, 2017, 3:02pm UTC](https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761 "2017-03-19T15:02:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)
#### Post date: [March 19, 2017, 3:02pm UTC](https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761/1 "2017-03-19T15:02:47Z")

</div>

I have nested tuples like

```julia
tup = Tuple{Tuple{Int64,Int8},Tuple{Int128,Int32}}

```

and I need to find the largest eltype of the elements, i.e. `Int128` in the above case. I feel I need to use `promote_type` in a recursive fashion, but my attempts have failed so far.

Thanks!

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [March 19, 2017, 3:18pm UTC](https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761/2 "2017-03-19T15:18:18Z")

</div>

What are your attempts so far?

---

<div class="post-metadata">

### Author: ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)
#### Post date: [March 19, 2017, 3:31pm UTC](https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761/3 "2017-03-19T15:31:24Z")

</div>

I think I got it. Here it is

```julia
using Base.Test

promote_tuple_type(a::Number) = typeof(a)
promote_tuple_type(a::Tuple) = promote_type(map(promote_tuple_type, a)...)

# basic tests
tup0 = ((1, 1), (1, 2), (2, 3), (2, 3, 4), ((2, 3, (3, 4, (5, 5, (1,))))))
tup1 = ((Int8(1), Int32(1)), (Int64(1), Float64(1)))
tup2 = ((Int8(1), Int32(1)), (Int64(1), Int128(1)))
tup3 = (((Int8(1), Int32(1)), (Int64(1), Int128(1))), (Float32(1), Float16(1)))

@test promote_tuple_type(tup0) == Int64
@test promote_tuple_type(tup1) == Float64
@test promote_tuple_type(tup2) == Int128
@test promote_tuple_type(tup3) == Float32

```

This works well, but maybe there is better way of doing it.

---

<div class="post-metadata">

### Author: ![gasagna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gasagna/32/1275_2.png) [@gasagna](https://discourse.julialang.org/u/gasagna)
#### Post date: [March 19, 2017, 3:45pm UTC](https://discourse.julialang.org/t/promote-type-of-nested-tuple-elements/2761/4 "2017-03-19T15:45:14Z")

</div>

The related problem is to construct a new tuple where all elements have the same type. I came up with two options

```julia
convert_tuple{T}(::Type{T}, a::Number) = T(a)
convert_tuple{T}(::Type{T}, a::Tuple) = (map(el->convert_tuple(T, el), a)...)
convert_tuple{T}(::Type{T}, a::Tuple) = (convert_tuple.(T, a)...)

```

The second looks nicer but incurs in more allocations than the first.

These works OK for me, but again, there might be a better way of doing it.
