Sub-Types

Sub-types πŸͺ 🐫

Sub-types are like breeds. They are a name for a type that already exist, that has all the same properties, but we want to differentiate for whatever reason (breeds and races are a human construct, not a biological one).

Just like Dromedaries are a Sub-Type of Camels

πŸͺ 🐫πŸͺ 🌴 🐫πŸͺ🐫πŸͺ🐫

subtype Dromedaries is Camel;

You can use it to identify numeric types.

subtype Hours is Positive range 1..24;
subtype Probability is Float range 0.0 .. 1.0;

And they can be combined with value types.

type Colors is  ( Red, Orange, Green, Blue );
subtype Streetlight is Red..Green;
Prime_Digits'('2');   -- This '2' is a Prime Digit

Every time values are assigned, the compiler checks that the value is in range, and forbids to be assigned a value out of the type. But sub-types can mix with the father type.

New types

Ada also allows to crate sub-types that cannot mix with the parent type. These are new types.

type Speed is new Float range 0.0 .. Light_Speed;

This allows to isolate behaviors of types, and avoid mixing types.