# Assignment to size zero array, \[in-place assign bug\]?

**URL:** <https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [February 29, 2020, 2:01am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312 "2020-02-29T02:01:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dehann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dehann/32/2003_2.png) [@dehann](https://discourse.julialang.org/u/dehann)\
**Post date:** [February 29, 2020, 2:01am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/1 "2020-02-29T02:01:50Z")

</div>

Hi, just came across this. Should assigning to a zero size array not perhaps produce an error:

```julia
julia> a = zeros(0,1)
julia> a[:,1] .= [1]
0-element view(::Array{Float64,2}, :, 1) with eltype Float64

```

Regular behavior does work:

```julia
julia> zeros(1,1)[:,1] .= [1]
1-element view(::Array{Float64,2}, :, 1) with eltype Float64:
 1.0

```

On Julia 1.3.1. Hope Internals & Design is the right venue for this.

---

<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 29, 2020, 2:43am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/2 "2020-02-29T02:43:33Z")

</div>

`zeros(0,1)` vs `zeros(1,1)` ?

---

<div class="post-metadata">

**Author:** ![dehann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dehann/32/2003_2.png) [@dehann](https://discourse.julialang.org/u/dehann)\
**Post date:** [February 29, 2020, 3:24am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/3 "2020-02-29T03:24:09Z")

</div>

I feel the the first assignment example should give an error, since it is not actually able to do the assignment (there is no memory to store the setting value). zeros(0,1) is completely flat, and assigning an array from a different size should not be allowed?

This example does work

```julia
a = zeros(2,3)
b = zeros(1,3)
b[1,:] = [1;2;3]
julia> a .= b
2×3 Array{Float64,2}:
 1.0 2.0 3.0
 1.0 2.0 3.0

```

So perhaps it is the intended behavior with repetition. If so then this is not a bug, but seems a bit odd.

---

<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:** [February 29, 2020, 6:51am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/4 "2020-02-29T06:51:46Z")

</div>

> [@dehann](#):
>
> I feel the the first assignment example should give an error, since it is not actually able to do the assignment (there is no memory to store the setting value)

It is able to assign to all elements. It’s just that there are none.

Cf

```julia
julia> all(isodd, zeros(0,1))
true

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [February 29, 2020, 1:30pm UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/5 "2020-02-29T13:30:59Z")

</div>

It’s not repetition, it’s [broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting-1), and your original example is broadcasting down to zero elements. But even if the target is empty, the dimensions must still satisfy the rules for broadcasting.

```julia
julia> a = zeros(0, 2)
0×2 Array{Float64,2}

julia> a .= ones(1, 1)
0×2 Array{Float64,2}

julia> a .= ones(1, 2)
0×2 Array{Float64,2}

julia> a .= ones(1, 3)
ERROR: DimensionMismatch("array could not be broadcast to match destination")
Stacktrace:
 [1] check_broadcast_shape at ./broadcast.jl:509 [inlined]
 [2] check_broadcast_shape at ./broadcast.jl:510 [inlined]
 [3] check_broadcast_axes at ./broadcast.jl:512 [inlined]
 [4] instantiate at ./broadcast.jl:259 [inlined]
 [5] materialize!(::Array{Float64,2}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2},Nothing,typeof(identity),Tuple{Array{Float64,2}}}) at ./broadcast.jl:823
 [6] top-level scope at none:1

```

Addendum:  
If you don’t want the broadcasting behavior, use regular assignment instead of dot assignment.

```julia
julia> a = zeros(0,1)
0×1 Array{Float64,2}

julia> a[:,1] = [1]
ERROR: DimensionMismatch("tried to assign 1-element array to 0×1 destination")
Stacktrace:
[...]

```

---

<div class="post-metadata">

**Author:** ![dehann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dehann/32/2003_2.png) [@dehann](https://discourse.julialang.org/u/dehann)\
**Post date:** [March 3, 2020, 9:42am UTC](https://discourse.julialang.org/t/assignment-to-size-zero-array-in-place-assign-bug/35312/6 "2020-03-03T09:42:55Z")

</div>

> Broadcasting to zero

Thanks, that’s a good description.
