# Bounds checking removal

**URL:** <https://discourse.julialang.org/t/bounds-checking-removal/6638>\
**Category:** General Usage\
**Created:** [October 24, 2017, 12:00am UTC](https://discourse.julialang.org/t/bounds-checking-removal/6638 "2017-10-24T00:00:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 24, 2017, 12:00am UTC](https://discourse.julialang.org/t/bounds-checking-removal/6638/1 "2017-10-24T00:00:56Z")

</div>

I am trying to provide `@inbounds` functionality for a custom type.

After reading the [docs](https://docs.julialang.org/en/latest/devdocs/boundscheck), I have experimented with various combinations of `checkbounds` with `@inline` and `@propagate_inbounds`. But still I can’t seem to get it to work, and I have a feeling I’m doing something quite stupid.

Why in the example below, doesn’t `@inbounds A[2]` work as expected?  
(It returns `nothing`)

```julia
# Julia v0.6.0

struct MyType end

#Base.checkbounds(A::MyType, i::Integer) = println("checking bounds ...")
Base.checkbounds(A::MyType, i::Integer) = i < 0 && throw(BoundsError(A, i))

@inline function Base.getindex(A::MyType, i::Integer)
    @boundscheck checkbounds(A, i)
    return 17
end

A = MyType()
A[2]
@inbounds A[2]

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [October 24, 2017, 1:08am UTC](https://discourse.julialang.org/t/bounds-checking-removal/6638/2 "2017-10-24T01:08:17Z")

</div>

1. `@inbounds` gives the compiler the freedom to remove the bound check. There’s no guarantee that it’ll actually do it. In particular, it’s not done in global scope where inlining is disabled.
2. `@inbounds` return `nothing` now but might change later.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [October 24, 2017, 1:37am UTC](https://discourse.julialang.org/t/bounds-checking-removal/6638/3 "2017-10-24T01:37:45Z")

</div>

Thank you Yichao.

yasm (Yet Another Stupid Mistake)

For the record, here’s a modified example showing `@inbounds` working as expected.

```julia
struct MyType end

Base.checkbounds(A::MyType, i::Integer) = println("checking bounds ...")

function Base.getindex(A::MyType, i::Integer)
    @boundscheck checkbounds(A, i)
    return 17
end

function f(A::MyType)
    s = 0
    for i in 1:10
        s += A[i]
    end
    return s
end

function g(A::MyType)
    s = 0
    for i in 1:10
        @inbounds s += A[i]
    end
    return s
end

A = MyType()
f(A) # performs bounds checking
g(A) # does not perform bounds checking

```
