fransvo@htsa.uucp (Frans van Otten) (01/25/89)
In Modula-2, an 'union' is defined like: TYPE Example = RECORD CASE : CARDINAL OF 1 .. 10 : a : INTEGER; b : BOOLEAN | 11 .. 15 : u : CHAR; v : ARRAY[1 .. 10] OF CHAR ELSE z : CHAR END END; Can anyone explain to me what the tag type and the caselabels are used for, apart from informing the reader what the intentions of the programmer might have been ? I know that a tag field can be included, but what is the real difference between an ordinary field and a tag field ? The contents of the tag field are variable and thus unknown at compile time, so the compiler can't check if you're using the correct variant. At run time this is not possible either because the computer can't tell which variant field you are accessing. -- Frans van Otten Algemene Hogeschool Amsterdam Technische en Maritieme Faculteit fransvo@htsa.uucp
broman@PEANUTS.NOSC.MIL (Vincent Broman) (01/26/89)
Good usage of variant records requires you do your own run-time checking of the tag. Essentially what is required is that references to fields that vary occur only inside CASE tag statements. As a relaxation to this rule one can infer the value of the tag from other parts of the program state and omit the CASE test, but this is error prone. Variant records without tag fields are just a special case of this relaxed usage. Vincent Broman, code 632, Naval Ocean Systems Center, San Diego, CA 92152, USA Phone: +1 619 553 1641 Internet: broman@nosc.mil Uucp: sdcsvax!nosc!broman
mark@hpcllmr.HP.COM (Mark Rozhin) (02/04/89)
one compiler (pascal, actually) supports an option that will make the compiler emit checking code to be executed at run-time to enure that you access the right variant. this can only be done of the tag is there, as opposed to just the tag type. notice that modula-3 solved the entire problem of variants and tags by removing them from the language. mr