13.07.2015 Views

apple-pascal-1.3-manual

apple-pascal-1.3-manual

apple-pascal-1.3-manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

o The limits on the number and types of fields within each variant part isthe same as for any Pascal record.o Identifiers may not be repeated among variants (this is why the fieldACCT became ACCTl and ACCT2 in our example).Free Union Variant RecordsIn an ordinary variant record, the tag field value tells your program how tointerpret the variant data. The tag field is particularly useful when thevariant fields are of different types; it helps safeguard againstmisinterpreting their values.However, you can create records in which you deliberately interpret fieldvalues in more than one way. A variant record can be declared without atag field identifier (but not without a tag field type); or the program maysimply ignore the tag field. In either case, the result is called a free unionvariant record. If the tag field identifier is omitted from the declaration,the tag field itself will be omitted from the resulting records. Omitting thetag field saves a little memory and makes the maneuver morestraightforward.Free union variant records provide a handy tool for converting data fromone type to another. Here's an example. The types INTEGER and.CHAR areboth stored in memory as words of 16 bits; suppose we wish to convertvalues of one into the other, or access their bits individually. We write thefollowing free union variant record declaration:VAR MAGICRECORD CASE INTEGER OF1 : ;2 : CSYMBOL CHAR>;3 : (BINARY PACKED ARRAY C0 .. 15l OF BOOLEAN)END;Notice that this declaration has no tag field identifier; however, it doesspecify that the tag field is type integer. Declaring a tag field type isnecessary so the Compiler can keep track of the three variant parts, whichare identified by the integers 1 through 3.When the record variable MAGIC is stored in memory, it occupies just 16bits because this is the length of each of its variant parts. But now thatsame 16-bit word can be accessed aso An integer, identified as MAGIC.NUMBER;o A CHAR variable, identified as MAGIC.SYMBOL; oro A packed array of 16 boolean values, one for each bit, identified asMAGIC.BINARY.The RECORD TypeIIl-55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!