That is such a useful feature! Surprised I haven’t seen that more often. So much fiddly code exists that’s just fixing offsets to conform to 0 (or 1 for Lua) -based indexing!
That is such a useful feature! Surprised I haven’t seen that more often. So much fiddly code exists that’s just fixing offsets to conform to 0 (or 1 for Lua) -based indexing!
// This also supports multidimensional arrays, that is why the parameters are arrays.
var array = Array.CreateInstance(elementType: typeof(Int32), lengths: [ 5 ], lowerBounds: [ -2 ]);
// This does not compile, the type is Int32[*], not Int32[].
// Console.WriteLine(array[0]);
array.SetValue(value: 42, index: -2);
Console.WriteLine(array.GetValue(-2));https://github.com/sourceryinstitute/fidbits/blob/master/src...
https://fortran-lang.discourse.group/t/just-say-no-to-non-de...
Hence why the whole base index discussion only became relevant in C based languages.
You're confusing the definition of the language with the implementation. In implementation you're right, most runtimes will treat arrays starting at 1 as a special case and optimize that access. The language itself doesn't make that distinction though. Here an array is simply any table indexed by integers. The documentation states it's thusly:
You can start an array at index 0, 1, or any other valueThe more you go away from raw pointer semantics the less intuitive it gets.
Yeah, that's mixing both of them. Wouldn't it work as well if they all used 1-based indexing or 0-based indexing? Sounds like the issue was that algorithms/stuff underneath wasn't 1-based.
Perhaps I've been influenced by writing a lot of code in assembler, way back when, but zero-based has always seemed completely natural to me, to the extent that I find it very hard to understand algorithms expressed in non-zero based code.
arr: array[-2..10] of integer;
pointer(@arr) == pointer(@arr[0])
Or you can use descriptors (dope vectors), but that involves quite an overhead. The books on compilers from the 70s (e.g. Gries's "Compiler construction for digital computers") have rather extensive discussions on both approaches.> The books on compilers from the 70s (e.g. Gries's "Compiler construction for digital computers")
Yellow cover, I think? My then GF bought it for Xmas at about 1984 or so. Not the best book on the topic, IMHO.
Now to get the 3rd element from array, you have to know the start index, so another parameter to pass to function.
EDIT (for more explanation): I have an input value from -10 to 100. I want to use this value to lookup something in a table. IN a ero indexed world I have to know what the lowest value is and subtract that from the input value to get to zero (so "another parameter to pass to function").
With an arbitrary start index the array is just indexed from the lowest value (-10). There is nothing more needing to be passed in.