# Making \`broadcast\` compile-time inference more accurate

**URL:** https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345
**Category:** General Usage
**Tags:** question
**Created:** [June 19, 2017, 11:15am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345 "2017-06-19T11:15:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 19, 2017, 11:15am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345/1 "2017-06-19T11:15:20Z")

</div>

Working on latest Julia, the following call is correctly inferred to be `Vector{Int64}`:

```julia
ww = [(1,2,3),(4,5,6)]
# this broadcast returns Vector{Int64}
broadcast(getindex,ww,1)

```

But,

```julia
qq = [(1,'a',1.0),(2,'b',2.0)]
broadcast(getindex,qq,1)

```

is only inferred weakly to be `Vector{Union{Int,Char,Float64}}`.  
This is annoying, since `broadcast(first,qq)` is inferred better.

What should I define to help type-inference? Making `broadcast(getindex,qq,Val{1})` accurately inferred is also sufficient (as without knowing the value 1 at compile-time there is actually no way to know which tuple element is chosen).

This can be useful in cases where a record is parsed into a multi-typed tuple.

---

<div class="post-metadata">

### Author: ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)
#### Post date: [June 19, 2017, 11:32am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345/2 "2017-06-19T11:32:07Z")

</div>

Hmm, I tried to reproduce it but failed:

```julia
v0.7.0-DEV.526> qq = [(1,'a',1.0),(2,'b',2.0)]
2-element Array{Tuple{Int64,Char,Float64},1}:
 (1, 'a', 1.0)
 (2, 'b', 2.0)

v0.7.0-DEV.526> broadcast(getindex,qq,1) |> typeof
Array{Int64,1}

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [June 19, 2017, 11:35am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345/3 "2017-06-19T11:35:00Z")

</div>

> [@Dan](#):
>
> Working on latest Julia

Do you mean 0.5.2? `broadcast` is much improved in Julia 0.6.

---

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [June 19, 2017, 11:43am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345/4 "2017-06-19T11:43:45Z")

</div>

latest Julia today means: Version 0.7.0-DEV.635.

but the problem is not with the return type, but with the _inferred_ return type i.e.

```julia
@code_warntype broadcast(getindex,qq,1)

```

is `Vector{Union{Int64,Char,Float64}}` but `broadcast(first,qq)` has `Vector{Int64}` in `@code_warntype` and I want `broadcast(getindex,qq,Val{1})` to do the same.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [June 20, 2017, 3:12am UTC](https://discourse.julialang.org/t/making-broadcast-compile-time-inference-more-accurate/4345/5 "2017-06-20T03:12:43Z")

</div>

Woof, that’s a hard one. You’re passing a value (`1`) rather than a type, and hoping that the type system will constant-propagate it. You might have more luck defining `mygetindex` to take a `Val{1}`. But beware that `Val` objects are full of traps for the unwary, see [https://docs.julialang.org/en/latest/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)-1](https://docs.julialang.org/en/latest/manual/performance-tips/#The-dangers-of-abusing-multiple-dispatch-(aka,-more-on-types-with-values-as-parameters)-1) and the section immediately above it.
