# Vectors with different out-of-bounds behavior

**URL:** <https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 26, 2020, 6:30pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817 "2020-01-26T18:30:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [January 26, 2020, 6:30pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/1 "2020-01-26T18:30:10Z")

</div>

Consider integer vectors, Ivect(n) = Vector{Int}(undef, n).

If you access a vector A = Ivect(n) with indices \< 1 or \> n,  
a BoundsError is thrown.

How can you introduce an integer vector type that simply returns 0 in these cases?

---

<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:** [January 26, 2020, 7:16pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/2 "2020-01-26T19:16:42Z")

</div>

There are in fact just two functions you must define for your struct to behave like an array:

```julia
struct Zeroed{T,N,AT} <: AbstractArray{T,N}
    data::AT
    Zeroed(data::AT) where {AT<:AbstractArray{T,N}} where {T,N} = new{T,N,AT}(data)
end
Base.size(A::Zeroed) = size(A.data)
Base.getindex(A::Zeroed, ij...) = all(in.(ij, axes(A.data))) ? 
    getindex(A.data, ij...) : zero(eltype(A))

Zeroed(rand(3,4))[3,100] == 0

```

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [January 26, 2020, 8:20pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/3 "2020-01-26T20:20:14Z")

</div>

Thanks, looks good, but exceeds my understanding. It would be enough if I got something like that going:

```
struct Ivect <: AbstractArray
    data::Vector{Int}
    function Ivect(n::Int)
        new(Vector{Int}(undef, n))
    end
end
Base.size(A::Ivect) = size(A.data)
Base.getindex(A::Ivect, n) = (n < 1 || n > size(A)) ? Int(0) : 
getindex(A.data, n) 

A = Ivect(5); for i in 1:size(A) (A[i] = i) end
println(A[0], " ", A[end], " ", A[end+1])

```

---

<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:** [January 26, 2020, 8:34pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/4 "2020-01-26T20:34:07Z")

</div>

This should almost work too, you need `struct Ivect <: AbstractArray{Int,1}` and I think you want `n > length(A)` in getindex. To write into the array, you also need

```julia
Base.setindex!(A::Ivect, val, i::Int) = A.data[i] = val

```

Then `A = Ivect(3); A[1] = 33; [A[i] for i in 0:10]` should work.

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [January 26, 2020, 9:12pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/5 "2020-01-26T21:12:00Z")

</div>

Great! Thanks! Now I can start studying your first, sophisticated, solution.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 26, 2020, 9:20pm UTC](https://discourse.julialang.org/t/vectors-with-different-out-of-bounds-behavior/33817/6 "2020-01-26T21:20:27Z")

</div>

You could also just use [`get`](https://docs.julialang.org/en/v1/base/collections/#Base.get) instead, e.g.:

```julia
julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> get(a, 2, 0)
2

julia> get(a, 5, 0)
0

julia> get(a, -1, 0)
0

```
