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
Input and Output
- Type MinuteType is not predefined - how to we do I/O with it?
- Must instantiate the package Ada.Text_IO.Modular_IO
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;
put(m);
Bitwise Operations
- 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;
y: Unsigned_Byte := u or v;
z: Unsigned_Byte := u xor v;
We must create a new package for IO, since MinuteType is newly defined
Type Checking
- Types with different names can not be mixed
Type Conversion
- Types with different names can not be mixed
with Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_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;
i, j: Integer;
begin
i := m * j; -- ????
put(m);
How to convert?
What does the convert do?