# Is there a /dev/null for AbstractArrays?

**URL:** <https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447>\
**Category:** General Usage\
**Created:** [October 29, 2019, 2:03pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447 "2019-10-29T14:03:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [October 29, 2019, 2:03pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/1 "2019-10-29T14:03:55Z")

</div>

Is there some type that I can pass into functions that would indicate that I don’t care about the result?

The background is that I always try to write in place functions. When I have `a, b, c = f(x, y, z)` I always implement it in terms of `f!(a, b, c, x, y, z)`. Note that all variables are arrays. Currently, if I don’t need `b` and `c`, I still have to allocate memory for them and the function has to fill them.

Is there some type that I could pass to the function indicating that I don’t need the variables? Something like `f!(a, nothing, nothing, x, y, z)`. The compiler could then produce a function that does not even do the computation for `b` and `c`, with code reduction and speedup as a result.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 29, 2019, 2:16pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/2 "2019-10-29T14:16:01Z")

</div>

You can check for `nothing` and not compute the output for those arguments, e.g.

```julia
function f!(a, b, c, x, y, z)
    if a !== nothing
        # fill a
    end
    if b !== nothing
        # fill b
    end
    if c !== nothing
        # fill c
    end
end

```

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [October 29, 2019, 2:18pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/3 "2019-10-29T14:18:13Z")

</div>

Indeed, but the compiler could easily do it for me. My question was more about whether it is possible in Julia.

---

<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:** [October 29, 2019, 2:21pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/4 "2019-10-29T14:21:50Z")

</div>

You could try to create a `DevNullArray` and see what kind of codegen you get. This is untested, but something like

```julia
struct DevNullArray{T,N,Ax<:NTuple{N,<:AbstractUnitRange{Int}}}
    axes::Ax
end

Base.setindex!(A::DevNullArray, v, I) = nothing

```

You’d need a few more methods, see [https://docs.julialang.org/en/latest/manual/interfaces/#man-interface-array-1](https://docs.julialang.org/en/latest/manual/interfaces/#man-interface-array-1).

If you try this and really like it, consider contributing it to [JuliaArrays](https://github.com/JuliaArrays).

---

<div class="post-metadata">

**Author:** ![haberdashPI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haberdashpi/32/26337_2.png) [@haberdashPI](https://discourse.julialang.org/u/haberdashPI)\
**Post date:** [October 29, 2019, 2:24pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/5 "2019-10-29T14:24:10Z")

</div>

Was just about to post essentially the same thing. Seems like it would be slightly better to define `setindex!` like so

```julia
Base.setindex!(A::DevNullArray, v, I...) = v

```

---

<div class="post-metadata">

**Author:** ![HenrikM](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrikm/32/17975_2.png) [@HenrikM](https://discourse.julialang.org/u/HenrikM)\
**Post date:** [October 29, 2019, 2:25pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/6 "2019-10-29T14:25:44Z")

</div>

Thank you for pointing me in the right direction! Perhaps `IgnoreArray` would be a better name?

---

<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:** [October 29, 2019, 3:41pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/7 "2019-10-29T15:41:57Z")

</div>

Personally, I love `DevNullArray`.

I would intepret `IgnoreArray` as an indication that one should ignore the array, which isn’t accurate.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 29, 2019, 3:56pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/8 "2019-10-29T15:56:16Z")

</div>

Note that this will cause code that reads from this array to break completely. Is probably obvious, but worth stating that you should only do this if you know how the function works.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 29, 2019, 3:59pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/9 "2019-10-29T15:59:58Z")

</div>

Another option is `WriteOnlyArray`. If reading is an error, well, then of course you can ignore the writes. 🙂

---

<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:** [October 29, 2019, 4:18pm UTC](https://discourse.julialang.org/t/is-there-a-dev-null-for-abstractarrays/30447/10 "2019-10-29T16:18:01Z")

</div>

While this is fun to brainstorm about, I believe that it will be very unlikely that everything folds down into nothing when using such an array in more complicated contexts. So just accepting a `Union{AbstractArray, Nothing}` and branching is most likely the way to go in practice.
