Variant Records

Variant Records - Basic Concepts

Variant Record for Transactions

    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;

Ada Code for Transactions

Motivation for Variant Records

Arrays of Variant Records


Dangers of Variant Records

Type Safety and Variant Records

Relation to Subtyping

Variant Records in Other Languages

The Term Union