# Using push!() on an array with 'nothing' as its only value results in error

**URL:** <https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188>\
**Category:** General Usage\
**Tags:** error\
**Created:** [July 16, 2020, 4:12pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188 "2020-07-16T16:12:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 16, 2020, 4:12pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/1 "2020-07-16T16:12:30Z")

</div>

I was trying to implement a stack using an array for which I had to create a single value array `[nothing]`. I needed `nothing` because my code included peeking the array. But when I tried to push an integer value into the array it resulted in an error. Here is the code and the error:

```julia
julia> stack = [nothing]
1-element Array{Nothing,1}:
 nothing

julia> push!(stack, 1)
ERROR: MethodError: convert(::Type{Union{}}, ::Int64) is ambiguous. Candidates:
  convert(::Type{Union{}}, x) in Base at essentials.jl:169
  convert(::Type{T}, x::Number) where T<:Number in Base at number.jl:7
  convert(::Type{T}, arg) where T<:VecElement in Base at baseext.jl:8
  convert(::Type{T}, x::Number) where T<:AbstractChar in Base at char.jl:179
Possible fix, define
  convert(::Type{Union{}}, ::Number)
Stacktrace:
 [1] convert(::Type{Nothing}, ::Int64) at ./some.jl:34
 [2] push!(::Array{Nothing,1}, ::Int64) at ./array.jl:912
 [3] top-level scope at REPL[11]:1

julia>

```

I want to use `nothing` similar to `None` in python. I am not sure if this is a bug or you can’t use nothing in this way.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 16, 2020, 4:23pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/2 "2020-07-16T16:23:51Z")

</div>

It’s a weird error but the problem is that `1` doesn’t fit within the eltype of your array:

```julia
julia> typeof(1) <: eltype(stack)
false

```

You can define one with a wider type, either just `[]` which has `Any`, or

```julia
julia> stack2 = Union{Nothing,Int}[]
0-element Array{Union{Nothing, Int64},1}

julia> push!(stack2, 1, nothing, 42)
3-element Array{Union{Nothing, Int64},1}:
  1
   nothing
 42

```

(I guess my first line is not the perfect way to think, as `push!(stack2, 1.0)` works… `push!` tries to convert if necessary.)

---

<div class="post-metadata">

**Author:** ![benninkrs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benninkrs/32/3191_2.png) [@benninkrs](https://discourse.julialang.org/u/benninkrs)\
**Post date:** [July 16, 2020, 4:35pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/3 "2020-07-16T16:35:55Z")

</div>

What are you hoping to achieve? Do you want the `nothing` to be replaced by the integer value? If so, then `push!` is not the right function to call – you should just assign the value to `stack[1]`. I am also wondering if an array is actually the most appropriate data structure for your goal.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [July 16, 2020, 4:36pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/4 "2020-07-16T16:36:11Z")

</div>

@mcabbott already posted the solution, so just to elaborate on his first reason. The OPs first line

```julia
stack = [nothing]

```

mentiones afterwars, that it generated an `Array{Nothing,1}`, that is an array whose elements (`eltype(stack)`) are only of type `Nothing`, so you can not push an integer (that’s not of type or a subtype of `Nothing`, clearly).

So with the `Union{Int, Nothing}[]`, the array consists of either Nothings or integers. then both are allowed to be pushed.  
The meta problem is, that from `nothing` Julia can only infere that the array is of nothings, but not the other types you want to put in there, that’s where you need the Union.

I hope that helps – and of course welcome to Julia discourse 🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [July 16, 2020, 5:30pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/5 "2020-07-16T17:30:35Z")

</div>

My gut feeling is that you probably shouldn’t be doing this, and that you should find a way to avoid putting a junk value in the vector.

So what do you mean by ‘peeking’ and why do you need to do it? Can you just check for `isempty` instead?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 16, 2020, 7:59pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/6 "2020-07-16T19:59:39Z")

</div>

I mean, it can be fine to use a sentinel value instead of `isempty` but then, it probably should be something of the same type, and that naturally would evaluate for the answer you want, like zero, a negative number, or the greatest/smallest Int (supposing your code will never reach it).

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 5:33am UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/7 "2020-07-17T05:33:26Z")

</div>

no i want the first element to be nothing because i later use comparison operators and i can’ t do that with an empty array.

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 5:37am UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/8 "2020-07-17T05:37:43Z")

</div>

That’s the problem, the code is dependent upon external input and i am not sure what else to put instead of nothing because the input can be any integer.

---

<div class="post-metadata">

**Author:** ![JaydevSR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaydevsr/32/31752_2.png) [@JaydevSR](https://discourse.julialang.org/u/JaydevSR)\
**Post date:** [July 17, 2020, 5:42am UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/9 "2020-07-17T05:42:20Z")

</div>

I just have a little question if anyone will please answer that. Is there any other way to impliment a stack so that I don’t have to predefine the types of the elements and the the stack will adapt according to whatever element gets passed in?

---

<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:** [July 17, 2020, 6:07am UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/11 "2020-07-17T06:07:42Z")

</div>

> [@JaydevSR](#):
>
> a stack so that I don’t have to predefine the types of the elements and the the stack will adapt according to whatever element gets passed in?

The simplest solution is using a `Any[]`. This will not “adapt”, but will be able to store all types from the beginning.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 17, 2020, 12:56pm UTC](https://discourse.julialang.org/t/using-push-on-an-array-with-nothing-as-its-only-value-results-in-error/43188/12 "2020-07-17T12:56:10Z")

</div>

Just to complement, `Any[]` will have worse performance than an array of specific type, but for something that reads element by element from an user input this is completely irrelevant.
