type
TPerson = record
FirstName, LastName: string[40];
BirthDate: TDate;
case Citizen: Boolean of
True: (Birthplace: string[40]);
False: (Country: string[20];
EntryPort: string[20];
EntryDate, ExitDate: TDate);
end;
type
TShapeList = (Rectangle, Triangle, Circle, Ellipse, Other);
TFigure = record
case TShapeList of
Rectangle: (Height, Width: Real);
Triangle: (Side1, Side2, Angle: Real);
Circle: (Radius: Real);
Ellipse, Other: ();
end;
For each record instance, the compiler allocates enough memory to hold all the fields in the largest variant. The
optional tag and the
constantLists (like
Rectangle
,
Triangle
, and so forth in the last example) play no role in the
way the
compiler manages the fields; they are there only for the convenience of the programmer.
The second reason for variant parts is that they let you treat the same data as belonging to different types, even in
cases where the compiler would not allow a typecast. For example, if you have a 64-bit Real as the first field in one
variant and a 32-bit Integer as the first field in another, you can assign a value to the Real field and then read back
the first 32 bits of it as the value of the Integer field (passing it, say, to a function that requires integer parameters).
Dostları ilə paylaş: