Typed indexes in Zig
34 points by lvig
34 points by lvig
So it's like Ada's:
type Column is range 0 .. Max_Column;
type Row_Index is range 0 .. Max_Row;
but ~35 years later?
Is this a question or a statement?
Oh, now I noticed my comment could be read as passive-aggressive. I'm sorry about that. Don't get me wrong: I love both Ada and Zig, and the work you're doing!
What was presented here reminds me of Ada. I feel like, still, Ada is a goldmine of great ideas. Anyway, I am personally happy Zig is moving in this direction.
I frequently find myself wishing for languages/standard-libraries to build-in the ability to say "this array is indexed by that integral (new-)type". You can build it yourself, but because the syntax is heavier at definition and use sites I often don't.
Admittedly, I do a lot of low-level performance-y work. If you're not worried about performance then your language's Map type is usually a way to get that type safety.
In Pascal (1970) an array's index type can be any bounded ordinal type, which includes enumerated types and subranges of an enumerated type.
If I recall correctly, Ada's arrays support this kind of thing. I'm not sure if any other remotely mainstream languages do (though it sure would be nice, as you say), though I'm sure someone will chime in with more examples.
Haskell's arrays are indexed by a type implementing the typeclass Ix, which can be any type where it makes sense to make a range of values.