# Check bounds function in julia?

**URL:** <https://discourse.julialang.org/t/check-bounds-function-in-julia/26212>\
**Category:** General Usage\
**Created:** [July 10, 2019, 8:03am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212 "2019-07-10T08:03:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsbsnyh](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dsbsnyh](https://discourse.julialang.org/u/dsbsnyh)\
**Post date:** [July 10, 2019, 8:03am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/1 "2019-07-10T08:03:14Z")

</div>

Hi, I have encountered this checkbounds function which confused me. Could anyone explain a bit what it does?

```julia

    a_star_algorithm(g::AbstractGraph{U},  
                    s::Integer,                       
                    t::Integer,                       
                    distmx::AbstractMatrix{T}=LightGraphs.weights(g),
                    heuristic::Function = (u,v) -> zero(T)) where {T, U}

**Arguments**
* `g` : graph object
* `S` : start vertex
* `t` : end vertex
* `distmx` : distance matrix
* `heuristic` : search heuristic function; by default returns zero 
"""
function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g
                          s::Integer, # the start vertex
                          t::Integer, # the end vertex
                          distmx::AbstractMatrix{T}=LightGraphs.weights(g),
                          heuristic::Function = (u,v) -> zero(T)) where {T, U}
    checkbounds(distmx, Base.OneTo(nv(g)), Base.OneTo(nv(g)))
    #.....

```

---

<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:** [July 10, 2019, 8:09am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/2 "2019-07-10T08:09:24Z")

</div>

Did you read the docs?

```julia
help?> checkbounds

  checkbounds(Bool, A, I...)

  Return true if the specified indices I are in bounds for the given
  array A. Subtypes of AbstractArray should specialize this method
  if they need to provide custom bounds checking behaviors; however,
  in many cases one can rely on A's indices and checkindex.

  See also checkindex.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> A = rand(3, 3);
  
  julia> checkbounds(Bool, A, 2)
  true
  
  julia> checkbounds(Bool, A, 3, 4)
  false
  
  julia> checkbounds(Bool, A, 1:3)
  true
  
  julia> checkbounds(Bool, A, 1:3, 2:4)
  false

  ────────────────────────────────────────────

  checkbounds(A, I...)

  Throw an error if the specified indices I are not in bounds for the
  given array A.

```

---

<div class="post-metadata">

**Author:** ![dsbsnyh](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dsbsnyh](https://discourse.julialang.org/u/dsbsnyh)\
**Post date:** [July 10, 2019, 8:26am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/3 "2019-07-10T08:26:07Z")

</div>

I read it. It’s just that it seems that the documentation of its usage is a bit different from the code I have seen above. So, I guess I will mainly focus on

```julia
checkbounds(A, I...)

  Throw an error if the specified indices I are not in bounds for the
  given array A.

```

Is it SO?

---

<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:** [July 10, 2019, 8:36am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/4 "2019-07-10T08:36:53Z")

</div>

> [@dsbsnyh](#):
>
> Is it SO?

Yes?

```julia
julia> A = [1, 2, 3];

julia> checkbounds(A, 2)

julia> checkbounds(A, 5)
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [5]

```

---

<div class="post-metadata">

**Author:** ![dsbsnyh](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dsbsnyh](https://discourse.julialang.org/u/dsbsnyh)\
**Post date:** [July 10, 2019, 8:39am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/5 "2019-07-10T08:39:08Z")

</div>

Thank you a lot then! So, it is mainly for the dimensions of the array bounds, not the bounds of the array values’ range.

---

<div class="post-metadata">

**Author:** ![dsbsnyh](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@dsbsnyh](https://discourse.julialang.org/u/dsbsnyh)\
**Post date:** [July 10, 2019, 9:43am UTC](https://discourse.julialang.org/t/check-bounds-function-in-julia/26212/6 "2019-07-10T09:43:21Z")

</div>

```julia
julia> Base.OneTo(2)
Base.OneTo(2)

help?> Base.OneTo()
  Base.OneTo(n)

  Define an AbstractUnitRange that behaves like 1:n, with the added
  distinction that the lower limit is guaranteed (by the type system)
  to be 1.

```

While we are on this, I was wondering, do you happen to know what else is this Base.OneTo() function used? It’s a little bit strange here…
