04-10-2012, 02:16 PM
Variant Records
Variant Records.docx (Size: 35.3 KB / Downloads: 26)
What are Variant Records?
In computer science, , variant record data structure used to hold a value that could take on several different, but fixed types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use. It can be thought of as a type which has several "cases," each of which should be handled correctly when that type is manipulated. Like ordinary unions, tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time
Variant RECORD TYPEs are RECORD TYPEs that support multiple record structures within a single record type.
Example: A geometric object record that has different components depending on whether the object is a rectangle (height and width) versus a circle (radius).
Operations on Variant Records
1. Retrieving field values:
o You can always retrieve the value of the discriminant
o You can always retrieve the values of fields in the fixed part
o You can only retrieve the values of fields in the variant part when the value of the discriminant matches the chosen variant
2. Storing field values:
o You can only store the value of the discriminant if:
1. You don't change the value, or
2. As part of a record assignment
o You can always store the values of fields in the fixed part
o You can only store the values of fields in the variant part when the value of the discriminant matches the chosen variant
3. Record assignment:
o A variant record value can always be assigned to an unconstrained variant variable of the correct record type
o A variant record value can only be assigned to a constrained variant variable (of the correct record type) if the discriminants match
4. Record comparison:
o You can only compare variant record types when the variants agree (Bah Humbug!)