13.6. Change of Representation

[PREVIOUS][UP][NEXT]

At most one representation clause is allowed for a given type and a given aspect of its representation. Hence, if an alternative representation is needed, it is necessary to declare a second type, derived from the first, and to specify a different representation for the second type.

Example:

  -- PACKED_DESCRIPTOR and DESCRIPTOR are two different types
  -- with identical characteristics, apart from their representation

  type DESCRIPTOR is
      record
        -- components of a descriptor
      end record;

  type PACKED_DESCRIPTOR is new DESCRIPTOR;

  for PACKED_DESCRIPTOR use
      record
        -- component clauses for some or for all components
      end record;

Change of representation can now be accomplished by assignment with explicit type conversions:

      D : DESCRIPTOR;
      P : PACKED_DESCRIPTOR;

      P := PACKED_DESCRIPTOR (D); -- pack D
      D := DESCRIPTOR(P);         -- unpack P

References: assignment, derived type, type, type conversion, type declaration, representation clause.


[INDEX][CONTENTS]