What is the most idiomatic way to implement bounds checking in `getindex` for arbitrary types?

I guess particular example is too simple. The explicit bounds check logic that I am actually working on is bit more involved; even if the explicit check fails, the index would still point to a valid memory location i.e. would pass the bounds checking of the default types.

Should I update the question to include a more representative example?