# Why does \`map\` of a named tuple and tuple return an array?

**URL:** <https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468>\
**Category:** General Usage\
**Created:** [October 30, 2019, 1:01am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468 "2019-10-30T01:01:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [October 30, 2019, 1:01am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/1 "2019-10-30T01:01:12Z")

</div>

`Array` seems like a strange return type for this interaction:

```julia
julia> map(+, (1, 2), (x=1, y=2))
2-element Array{Int64,1}:
 2
 4

```

Why not tuple?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 30, 2019, 7:12am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/2 "2019-10-30T07:12:29Z")

</div>

Because it falls back to the general iterable collection method.

A special method for this could be added, but it may be considered breaking. I think that for mixed arguments, `map` falling back to `collect` is fine.

A workaround is

```julia
julia> map(+, (1, 2), values((x=1, y=2)))
(2, 4)

```

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [October 30, 2019, 7:18am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/3 "2019-10-30T07:18:46Z")

</div>

I was figuring that was the reason, and changing it now would be breaking. But it just seems like  
it should return some kind of tuple - that’s the least surprising option. The workaround is easy, just debugging the issue the first time it happens is weird.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 30, 2019, 7:22am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/4 "2019-10-30T07:22:11Z")

</div>

> [@Raf](#):
>
> But it just seems like  
> it should return some kind of tuple - that’s the least surprising option.

I am not sure — one could argue for a `NamedTuple`.

I think it is best if `map` tries to preserve type and structure for _homogeneous_ arguments, and falls back to an `Array` for _heterogeneous_ ones. This is easy to reason about.

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [October 30, 2019, 7:31am UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/5 "2019-10-30T07:31:23Z")

</div>

But array allocates and isn’t type stable

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [November 7, 2019, 9:03pm UTC](https://discourse.julialang.org/t/why-does-map-of-a-named-tuple-and-tuple-return-an-array/30468/6 "2019-11-07T21:03:21Z")

</div>

Maybe file an issue so that we track it for Julia 2.0.
