- Sat 02 September 2017
- Jacob Sparre Andersen
- #contracts, #private types, #predicates
Sometimes, you want to declare a subtype for a subset of the possible values of a private type.
You might for example want a subtype of Ada.Calendar.Time, which only can include times in the past. If Ada.Calendar.Time was a numeric type, we could declare a subset using range, but as it is a private type, we have to do it differently:
subtype Past_Time is Ada.Calendar.Time with Dynamic_Predicate => Past_Time < Ada.Calendar.Clock;
You could even expand this to a subtype representing values in the last hour:
subtype Last_Hour is Past_Time with Dynamic_Predicate => Ada.Calendar.Clock - 3600.0 <= Last_Hour;
Notice that while Past_Time is an ever expanding subtype, Last_Hour is an ever changing subtype, such that values which were valid earlier, not necessarily are valid now.