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
OffsetArrays, and we can totally write1:nsyntax in that case. We even have therequire_one_based_indexingfunction to check inputs. Funnily enough, if there’s a custom array type that isn’t 1-based,OffsetArrays can make it 1-based to be compatible with your 1-based code. -
OffsetArrays 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
OffsetArrays work inAbstractArraycode? Many functions really could work with any indices, and some functions currently marked withrequire_one_based_indexingactually could work with offset indices, just require that all inputs share compatible ones. Incompatible indices can just throw the existingDimensionMismatcherror. -
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
OffsetArraydiscussion 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 frombegintoend(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 annmeaning the last index vs. annmeaning 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/endsyntax 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.