# How is the output type determined in map and friends?

**URL:** <https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096>\
**Category:** General Usage\
**Created:** [August 26, 2018, 9:53pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096 "2018-08-26T21:53:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [August 26, 2018, 9:53pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/1 "2018-08-26T21:53:35Z")

</div>

Just curious, how is the container type determined for `map`?

It seem pretty magical to me:

```julia
julia> @code_warntype map(x -> x/2, rand(Int, 5))
Body::Array{Float64,1}
1995 1 ─ %1 = %new(Base.Generator{Array{Int64,1},getfield(Main, Symbol("##3#4"))}, getfield(Main, Symbol("##3#4"))(), A)::Base.Generator{Array{Int64,1},getfield(Main, Symbol("##3#4"))} │╻╷ Type
     │ %2 = invoke Base._collect(_3::Array{Int64,1}, %1::Base.Generator{Array{Int64,1},getfield(Main, Symbol("##3#4"))}, $(QuoteNode(Base.EltypeUnknown()))::Base.EltypeUnknown, $(QuoteNode(Base.HasShape{1}()))::Base.HasShape{1})::Array{Float64,1}
     └── return %2 

```

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [August 26, 2018, 9:56pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/2 "2018-08-26T21:56:29Z")

</div>

I guess it boils down to this: how is the output type of functions determined before they are evaluated in regular Julia? I understand that this is definitely something that the compiler should be able to figure out, but how would I be able to write a function that uses the compiler’s smarts to pre-allocate an output array?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 26, 2018, 10:08pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/3 "2018-08-26T22:08:00Z")

</div>

> [@galenlynch](#):
>
> the output type of functions determined before they are evaluated in regulat Julia

It isn’t. The inference type is used for the empty case.

> [@galenlynch](#):
>
> but how would I be able to write a function that uses the compiler’s smarts to pre-allocate an output array

Don’t. Not for any non-empty case or at least not without making sure your code runs just fine without it.

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [August 26, 2018, 10:11pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/4 "2018-08-26T22:11:16Z")

</div>

Good advice to keep it simple, but it looks like the inference type is used by map. I’m just curious how it works.

```julia
julia> @code_warntype map(x -> x/2, rand(Int, 0))
Body::Array{Float64,1}
1995 1 ─ %1 = %new(Base.Generator{Array{Int64,1},getfield(Main, Symbol("##5#6"))}, getfield(Main, Symbol("##5#6"))(), A)::Base.Generator{Array{Int64,1},getfield(Main, Symbol("##5#6"))} │╻╷ Type
     │ %2 = invoke Base._collect(_3::Array{Int64,1}, %1::Base.Generator{Array{Int64,1},getfield(Main, Symbol("##5#6"))}, $(QuoteNode(Base.EltypeUnknown()))::Base.EltypeUnknown, $(QuoteNode(Base.HasShape{1}()))::Base.HasShape{1})::Array{Float64,1}
     └── return %2   

```

---

<div class="post-metadata">

**Author:** ![galenlynch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galenlynch/32/17947_2.png) [@galenlynch](https://discourse.julialang.org/u/galenlynch)\
**Post date:** [August 26, 2018, 10:20pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/5 "2018-08-26T22:20:49Z")

</div>

oh nevermind, I guess the `code_warntype` basically says how it’s done.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 26, 2018, 10:31pm UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/6 "2018-08-26T22:31:27Z")

</div>

> [@galenlynch](#):
>
> it looks like the inference type is used by map

Yes, but

> [@yuyichao](#):
>
> Not for any non-empty case or at least not without making sure your code runs just fine without it.

---

<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:** [August 27, 2018, 6:07am UTC](https://discourse.julialang.org/t/how-is-the-output-type-determined-in-map-and-friends/14096/7 "2018-08-27T06:07:37Z")

</div>

BTW, with [union splitting](https://julialang.org/blog/2018/08/union-splitting), I wonder if this is still needed. `map` & friends could just return `Any[]` for empty collections.
