Can I skip `@boundscheck` within a function

Hi, suppose I am using a function

function f(x,y)
	@boundscheck size(x) == size(y) || error("sizes do not match")
	x'y
end

written by someone else and I want to use arguments x = rand(2,2); y = rand(2). Is there a way to suppress the @boundscheck so that I can call the function with the aforementioned arguments without triggering the error?

use @inbounds when calling the function…

But someone is doing something wrong if this is what you have to do.

I’ve tried @inbounds f(x,y) both in the console and as part of a file I include(...) and the error still appears.

julia> function f(x,y)
               @boundscheck size(x) == size(y) || error("sizes do not match")
               x'y
       end
f (generic function with 1 method)

julia> g(x,y) = @inbounds f(x,y)
g (generic function with 1 method)

julia> f(rand(2,2), rand(2))
ERROR: sizes do not match
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:33
 [2] f(x::Matrix{Float64}, y::Vector{Float64})
   @ Main ./REPL[6]:2
 [3] top-level scope
   @ REPL[8]:1

julia> g(rand(2,2), rand(2))
2-element Vector{Float64}:
 0.5974016405805198
 0.49780565668703575
1 Like

Thank you very much.