# How to define a Julia Vector with only integers ≥ 0

**URL:** <https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894>\
**Category:** General Usage\
**Tags:** question, type, data\_structures\
**Created:** [October 4, 2024, 7:54am UTC](https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894 "2024-10-04T07:54:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brinkhuis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brinkhuis/32/18752_2.png) [@Brinkhuis](https://discourse.julialang.org/u/Brinkhuis)\
**Post date:** [October 4, 2024, 7:54am UTC](https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894/1 "2024-10-04T07:54:39Z")

</div>

As input for a function, I need a Vector with values greater than or equal to zero.

I tried `l = Array{>:UInt8, 1}`

However `push!(l, 9`) throws an error.  
`MethodError: no method matching push!(::Type{Vector{>:UInt8}}, ::Int64)`

What is the right way to define such an array?

Use case:

```
function concatenate_integers(l::Array{Int, 1})
    """
    concatenate_numbers([3, 4, 5]) == 345
    """
    @assert all(i -> i ∈ 0:9, l) "Only integers in range 0:9 are valid"
    s = 0 # sum
    for (i, n) in enumerate(reverse(l))
        s += n * 10^(i-1)
    end
    return s
end

```

I would like to replace the `all(i -> i ∈ 0:9, l)` part by enforcing the right input type.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 4, 2024, 7:56am UTC](https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894/2 "2024-10-04T07:56:50Z")

</div>

> [@Brinkhuis](#):
>
> However `push!(l, 10`) throws an error.

It’s because `1` is an `Int` and not a `UInt`

```julia
julia> a = UInt[]
UInt64[]

julia> push!(a, UInt(1))
1-element Vector{UInt64}:
 0x0000000000000001

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [October 5, 2024, 1:37am UTC](https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894/4 "2024-10-05T01:37:50Z")

</div>

UInt8  
smallest value = 0  
largest value = 255

make sure you are HAPPY with the limitations.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 5, 2024, 4:18am UTC](https://discourse.julialang.org/t/how-to-define-a-julia-vector-with-only-integers-0/120894/5 "2024-10-05T04:18:30Z")

</div>

> [@Brinkhuis](#):
>
> `@assert all(i -> i ∈ 0:9, l) "Only integers in range 0:9 are valid"`

> [@Brinkhuis](#):
>
> I would like to replace the `all(i -> i ∈ 0:9, l)` part by enforcing the right input type.

If you _really_ want to do this by relying on the type system, my package TypeDomainNaturalNumbers.jl is the solution. However it’s not clear whether this would fit your, unknown, use case.
