JS:

It would be nice to be able to use enums in a hierarchical way:

enum colors
{
     enum Red { RedOrange, ... }
     enum Green { GreenBlue, ...}
     enum Blue { BlueYellow, ... }
...
}

which would be the same as the flattened version,

enum colors
{
Red, RedOrange, ..., Green, GreenBlue, ..., Blue, BlueYellow, ..., ...
}

but we could dereference such as

colors.Red.RedOrange,
colors.Blue,
colors.Green.GreenBlue,

(This isn't a great example but demonstrates what I would like to be able to do)

Is anything like this possible?

In Ada you can define ranged types, and there is a keyword to define their subtypes:

type Day is
   (Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday);

-- Derived types, they are new incompatible with the original:
type Business_Day is new Day range Monday .. Friday;
type Weekend_Day is new Day range Saturday .. Sunday;

-- Subtypes that convert implicitly to their supertype:
subtype Business_Day is Day range Monday .. Friday;
subtype Weekend_Day is Day range Saturday .. Sunday;
subtype Dice_Throw is Integer range 1 .. 6;

The Ada compiler enforces static typing where possible, and uses dynamic tests where it can't (and the dynamic tests can be disabled with a compiler switch). So if assign a generic integer to a Dice_Throw, the Ada compiler performs a run-time test to see if it's in the correct range.

Ada code is based on similar things that avoid/catch lot of bugs.

Bye,
bearophile

Reply via email to