-- Illustrates variant records
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure variant is
type PaymentType is (Cash, Check, Credit);
-- The_Type is called the discriminant of the type
type Transaction(The_Type: PaymentType := Cash) is record
Amount: Integer;
case The_Type is
when Cash =>
Discount: boolean;
when Check =>
CheckNumber: Positive;
when Credit =>
CardNumber: String(1..5);
Expiration: String(1..5);
end case;
end record;
t: Transaction; -- Default is cash transaction
begin
-- All transactions have an amount field
put(t.amount);
-- Cash transactions have a discount field
if t.discount then
put("Give a discount");
else
put("No discount");
end if;
-- Create a new credit transaction
t := (credit, 100, "12345", "01/05");
t.amount := 200;
put(t.amount);
put(t.CardNumber);
put(t.Expiration);
put(t.CheckNumber); -- Compiles but raises constraint error at runtime
-- t.The_Type := check; -- Compile error.
-- When changing discriminant, the entire record must be assigned.
end variant;