# Type-stable broadcasting involving nested array

**URL:** <https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353>\
**Category:** New to Julia\
**Tags:** question, code\_warntype, type-stability\
**Created:** [February 28, 2023, 8:32pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353 "2023-02-28T20:32:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [February 28, 2023, 8:32pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/1 "2023-02-28T20:32:32Z")

</div>

Hi Folks,

I am trying to get the following toy snippet type-stable, but wasn’t sure how to proceed.

```julia
using Random
using InteractiveUtils

using LinearAlgebra

const T = Float64

# scalar ver
function foo(
  a ::D,
  b ::AbstractVector{T};
  c ::AbstractVector{T}=similar(b),
) where {D<:Real, T<:Real}
  c[begin] = b[begin] + a
  return last(c)
end

# vector ver
function vfoo(
  va ::AbstractArray{D},
  vb ::AbstractArray{<:AbstractVector{T}};
  vc ::AbstractArray{<:AbstractVector{T}}=similar(b),
) where {D<:Real, T<:Real}

  ((a, b, c)->foo(a, b; c=c)).(va, vb, vc)

  return last.(vc)
end

# scalars
a = T(randn())
b = [T(randn())]
c = [T(randn())]

@code_warntype foo(a, b; c=c)

# vectors
va = [copy(a), copy(a)]
vb = [copy(b), copy(b)]
vc = [copy(c), copy(c)]

@code_warntype vfoo(va, vb; vc=vc)

```

The second `@code_warntype` complains about:

```julia
::Type{AbstractArray{var"#s6"<:AbstractVector{Float64}}}

```

and I don’t know how to deal with it.

Thanks ahead for your time and help!

BTW, links recommending tutorials of understanding `@code_warntype` outputs would also be very much appreciated!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 28, 2023, 8:59pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/2 "2023-02-28T20:59:47Z")

</div>

if you remove all of your type annotations it’s actually stable

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 28, 2023, 9:00pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/3 "2023-02-28T21:00:32Z")

</div>

> [@tianrluo](#):
>
> `::Type{AbstractArray{var"#s6"<:AbstractVector{Float64}}}`

you didn’t copy the full line:

```julia
│ %8 = Core.apply_type(Main.AbstractArray, @_6::Core.Compiler.PartialTypeVar(var"#s6"<:AbstractVector{Float64}, true, true))::Type{AbstractArray{var"#s6"<:AbstractVector{Float64}}}

```

you see `apply_type`? your annotations are doing de-optimization

---

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [February 28, 2023, 9:03pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/4 "2023-02-28T21:03:50Z")

</div>

thanks for looking into this!

I thought it was a good practice to be specific on the intended usage of the function.

Is there a correct way to type-annotate `vfoo`?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 28, 2023, 9:08pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/5 "2023-02-28T21:08:15Z")

</div>

> [@tianrluo](#):
>
> Is there a correct way to type-annotate `vfoo`?

maybe but why? annotating functions don’t speed them up, and only limit their applicability. if you’re mostly a user, you shouldn’t annotate too much

---

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [February 28, 2023, 9:14pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/6 "2023-02-28T21:14:52Z")

</div>

> [@jling](#):
>
> maybe but why? annotating functions don’t speed them up, and only limit their applicability. if you’re mostly a user, you shouldn’t annotate too much

I was actually trying to make a package, and hoped to limit the API.

Was imagining users would be less confused if (type-stability) error shows at the top level they call, instead of during the broadcasting.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 28, 2023, 9:54pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/7 "2023-02-28T21:54:58Z")

</div>

Have you tried this?

```julia
function vfoo(
    va::AbstractArray{D},
    vb::AbstractArray{A}, ;
    vc::AbstractArray{B}=similar(b),
) where {D<:Real, A<:AbstractVector{T}, B<:AbstractVector{T}} where T<:Real

    ((a, b, c)->foo(a, b; c)).(va, vb, vc)
    return last.(vc)
end

```

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [February 28, 2023, 9:55pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/8 "2023-02-28T21:55:39Z")

</div>

Why would you want to limit your API ? This seems like a shame : if someone want to use your code for something you did not know it could be used for, then they can’t.

---

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [February 28, 2023, 9:57pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/9 "2023-02-28T21:57:18Z")

</div>

> [@lrnv](#):
>
> Why would you want to limit your API ? This seems like a shame : if someone want to use your code for something you did not know it could be used for, then they can’t.

I need to write an `rrule` for my API.

---

<div class="post-metadata">

**Author:** ![tianrluo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tianrluo/32/7629_2.png) [@tianrluo](https://discourse.julialang.org/u/tianrluo)\
**Post date:** [February 28, 2023, 9:59pm UTC](https://discourse.julialang.org/t/type-stable-broadcasting-involving-nested-array/95353/10 "2023-02-28T21:59:02Z")

</div>

Awesome!

Would you also have time to share about how this one was different from the original one?  
The type annotation of `julia` has been quite confusing for me, espacially those involving `Abstract` types…
