Hacker News new | ask | show | jobs
by ilove_banh_mi 844 days ago
Ada has excellent enumeration types, and subtypes, and compile-time checking of case statement coverage for enum values, and an optional representation mechanism to control the binary value for each (symbolic) enum value. I'm not aware of any other language with that kind of enum type system.

See e.g. https://adaic.org/resources/add_content/docs/craft/html/ch05...

3 comments

Not only that, but when you define an enum you get 'Pred and 'Succ to move between values, range iteration over all values with 'Range and 'First and 'Last, string conversion with 'Image and parsing with 'Value.
Ada rocks. It's just about the most underappreciated language ever.
Rust I think covers most if not all of Ada features you mentioned
Rust's "enum" mechanism is really an algebraic data type, and corresponds to Ada's enumeration types when applied as discriminants in variant record types. But Ada's enumeration types have a wider context of use, separate and independent from variant record types, including compile- and run-time features related directly to a) symbolic order and symbol names, b) binary representation mapped to these symbols, and c) subtyping/ranges.

I'm not aware of a good online presentation focused exclusively on Ada's enumeration types and their various uses. It's not even singled out in the Rationale documents for the design of the language and the (3?) design revisions since the 1980 launch; maybe the AARM (Annotated Ada Reference Manual) has more focused discussions? I'm not sure, I haven't looked at these since ~20y ago.

Rust enum model is a hybrid.

It allows to set discriminants explicitly and it allows unit-only enums (enums with only discriminants and no structures associated with them). You can control the underlying type of the discriminant too.

With a little bit of derive macro sugar you can even iterate through all the values of enum.