As someone who was in the “1-based to rule them all” camp for a while during that discussion, I can totally see the appeal of separating OffsetArrays
somehow. But I came to realize some things:
-
There’s no decree to use or support
OffsetArray
s, and we can totally write1:n
syntax in that case. We even have therequire_one_based_indexing
function to check inputs. Funnily enough, if there’s a custom array type that isn’t 1-based,OffsetArray
s can make it 1-based to be compatible with your 1-based code. -
OffsetArray
s are actually important. You should see the cool examples on the Julia blog post introducing them, but it’s not hard to run into math where starting at 1 doesn’t make sense. We’d have to do offset calculations to get to the index we need anyway, why not make a wrapper type do it automatically? -
Why shouldn’t
OffsetArray
s work inAbstractArray
code? Many functions really could work with any indices, and some functions currently marked withrequire_one_based_indexing
actually could work with offset indices, just require that all inputs share compatible ones. Incompatible indices can just throw the existingDimensionMismatch
error. -
We definitely should make a docs section or tutorial that teaches people right away how to write “generic” indices; people might not be as good as indexing arrays as they think. (Even the devs! The
OffsetArray
discussion revealed some overflow bugs when indices or related values get too close totypemax
/typemin
.) It’s not a big deal when you’re just indexing frombegin
toend
(and in that case, just calleachindex(A)
oraxes(A, i)
), but it gets real important when you start doing fancy stuff like indexing the “middle”, skipping every other index, or going backwards. Obscure errors happen when different key quantities that happen to have the same values are confused with each other, like ann
meaning the last index vs. ann
meaning length. In 0-based indexing, this is actually more annoying because there are invisible0+
everywhere that you have to spot to edit the indexing order.begin/end
syntax could be improved to make these distinctions easier, and even if you don’t do offset indices you’d reach for the generic syntax just for clarity.