Hacker News new | ask | show | jobs
by ainar-g 844 days ago
A feature that combines well with range and enum types are typed indexes of arrays:

  TYPE
   TWeekday = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
    Sunday);
  VAR
   WeekdayNameIndex : ARRAY [TWeekday] OF ShortString = ('Mon', 'Tue',
    'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
Now `WeekdayNameIndex` is a properly type- and range-checked look-up table.

It's really sad that these simple joys of Pascal are still lacking from a lot of mainstream languages.

2 comments

This is an awesome feature, combined with appropriate type and bounds checking and prevents so many errors. It can also avoid resorting to a heavier-weight map type.

Ada has this as well, including using any arbitrary continuous range for array indexing which handles remapping indices for you. e.g. if the key range is 20-40 the language handles associating it with array indices for you.

Indeed, I used that kind of stuff a lot.
I also used it, but wouldn't go back.
Me neither, but that is because now I have F#, Scala, Haskell, Rust,... :)