Modular Types
Modular Types
Modular Types
- Example modular type: minutes.adb
(prettified)
- Values of type
minuteType
range from 0 to 59 and they wrap
- Arithmetic for type
minuteType
wraps
with Ada.Text_IO;
procedure minutes is
type MinuteType is mod 60;
package Minute_IO is new Ada.Text_IO.Modular_IO (Num => MinuteType);
use Minute_IO;
m: MinuteType;
begin
m := 50;
m := m + 20;
put(m); -- Outputs 10
Side Note: Modular types allow some bitwise operations such as and, or, not, xor:
type Unsigned_Byte is mod 256;
u: Unsigned_Byte := 2#0011_0101#
v: Unsigned_Byte := 2#1111_0000#
w: Unsigned_Byte := u and v;
We must create a new package for IO, since MinuteType is newly defined
Type Checking
- Types with different names can not be mixed