# Functions copyto! and splat don't work with arrays with #undef 'values'

**URL:** https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322
**Category:** General Usage
**Tags:** question, proposal
**Created:** [June 14, 2019, 8:18pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322 "2019-06-14T20:18:32Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Codyk12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codyk12/32/15392_2.png) [@Codyk12](https://discourse.julialang.org/u/Codyk12)
#### Post date: [June 14, 2019, 8:18pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322/1 "2019-06-14T20:18:32Z")

</div>

The copyto!() function and splat(…) are unable to handle arrays with uninitialized values.

Is this supposed to not work or reasons I’m not seeing, or should this be fixable?

Here is an example:

```julia
julia> a = Array{String}(undef, 3)
3-element Array{String,1}:
 #undef
 #undef
 #undef

julia> a[1] = "a"
"a"

julia> b = Array{Any}(undef, 3)
3-element Array{Any,1}:
 #undef
 #undef
 #undef

julia> copyto!(b,a)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex at ./array.jl:729 [inlined]
 [2] copyto!(::IndexLinear, ::Array{Any,1}, ::IndexLinear, ::Array{String,1}) at ./abstractarray.jl:753
 [3] copyto!(::Array{Any,1}, ::Array{String,1}) at ./abstractarray.jl:745
 [4] top-level scope at none:0

julia> print(a...)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 14, 2019, 8:21pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322/2 "2019-06-14T20:21:26Z")

</div>

You cannot assign `#undef` to something so the behavior for `copyto!` looks correct.

For the splatting, you are effectively trying to do `print(a[1], a[2], a[3])` which accesses the `#undef` and thus errors.

What would the “fix” here be? `#undef` is not a first class value in Julia.

---

<div class="post-metadata">

### Author: ![Codyk12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codyk12/32/15392_2.png) [@Codyk12](https://discourse.julialang.org/u/Codyk12)
#### Post date: [June 14, 2019, 8:28pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322/3 "2019-06-14T20:28:31Z")

</div>

Should have mentioned what the fixes would be.

for copyto!  
it would copy the data in the array, and skip over the undef values:

```julia
julia> a = Array{String}(undef, 3)
3-element Array{String,1}:
 #undef
 #undef
 #undef

julia> a[2] = "a"
"a"

julia> a
3-element Array{String,1}:
 #undef
    "a"
 #undef

julia> b = Array{Any}(undef, 3)
3-element Array{Any,1}:
 #undef
 #undef
 #undef

julia> copyto!(b,a)
3-element Array{String,1}:
 #undef
    "a"
 #undef

```

This might make more sense in the context of the BSON library and issue [https://github.com/MikeInnes/BSON.jl/issues/43](https://github.com/MikeInnes/BSON.jl/issues/43) and pull request [https://github.com/MikeInnes/BSON.jl/pull/47](https://github.com/MikeInnes/BSON.jl/pull/47)

We were able to work around it (there were issues with saving trained flux models) and these issues would make saving and reading partial arrays a lot easier.

for print(a…):

```julia
julia> print(a...)
#undefa#undef

```

more particularly for function calls

```julia
julia> f(a...) = print(a...)
f (generic function with 1 method)

julia> f(a)
[#undef, "a", #undef]

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 14, 2019, 8:44pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322/4 "2019-06-14T20:44:18Z")

</div>

> [@Codyk12](#):
>
> it would copy the data in the array, and skip over the undef values:

That would be surprising to me, and might hide bugs in the code.

> [@Codyk12](#):
>
> We were able to work around it

Yeah, I think explicitly handling `#undef` when needed is the way to go.

> [@Codyk12](#):
>
> for print(a…):
> 
> ```julia
> julia> print(a...)
> #undefa#undef
> 
> ```

`#undef` is just the way that arrays and structs decide to print their non assigned elements. There is no way to have an object that is `#undef` so you can’t even write the `print` function you envision.

---

<div class="post-metadata">

### Author: ![Codyk12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/codyk12/32/15392_2.png) [@Codyk12](https://discourse.julialang.org/u/Codyk12)
#### Post date: [June 14, 2019, 8:46pm UTC](https://discourse.julialang.org/t/functions-copyto-and-splat-dont-work-with-arrays-with-undef-values/25322/5 "2019-06-14T20:46:43Z")

</div>

Alright, that makes sense.

Thanks for the responses.
