checkbounds(::Type{Bool}, ::Integer, index)

checkbounds(::Type{Bool}, dimlength::Integer, index)

Return a Bool describing if the given index is within the bounds of the given dimension length. Custom types that would like to behave as indices for all arrays can extend this method in order to provide a specialized bounds checking implementation.

Examples

julia> arr = [1, 2, 3, 4, 5];

julia> checkbounds(arr, 3)
3

julia> checkbounds(arr, 6)
ERROR: BoundsError: attempt to access 5-element Vector{Int64} at index [6]

In the above example, the checkbounds function is used to verify if the specified index is within the bounds of the array. If the index is valid, it returns the index itself. However, if the index is out of bounds, it throws a BoundsError with an appropriate error message.

Common mistake example:

julia> arr = [10, 20, 30];
julia> checkbounds(arr, 0)
ERROR: BoundsError: attempt to access 3-element Vector{Int64} at index [0]

In this example, the index provided is 0, which is not a valid index for an array in Julia. Remember that Julia uses 1-based indexing, so the valid indices for an array of length n are from 1 to n. Always ensure that the indexes passed to checkbounds are within the correct range to avoid such errors.

See Also

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: