4 ms·
Pascal, and Pascal-derived languages such as Modula and Turbo Pascal, have this: type Age = 1..199; A value of type Age can only have the values 1 <= x <=
by lobster_johnson 12y ago
Pascal, and Pascal-derived languages such as Modula and Turbo Pascal, have this:
type Age = 1..199;
A value of type Age can only have the values 1 <= x <= 199, and can't be assigned to non-Age variables. Enums belong to this category of ordinal types.
In Ada:
type Age is Range 1 .. 199;
type Temperature is new Integer Range 32 .. 212;
a: Age; t: Temperature
a := t -- this fails at compile time
Ada 2012 adds the support for specifying Eiffel-style contracts, but on types:
subtype Even is Integer
with Dynamic_Predicate => Even mod 2 = 0;
You can't assign an odd number to a variable of type Even.
- mamcx 12y agoYep, I know about pascal (my 2d language after foxpro) but not the ADA 2012. The Dynamic_Predicate is on runtime or compile time? If assign a:Int = Even, I need to be explicit and do a convert (I imagine is yes, that is the pascal style but not have experience with ADA)
- lobster_johnson 12y agoI believe it occurs only at compile time if it is possible for the compiler to evaluate the check; otherwise it's done at runtime.