FutureBasic Logo

<<    Index    >> FutureBasic 5

begin union   statement



Syntax:
begin record recordName
  dim
statements...
  begin union
    dim
statements
  end union
  dim
statements...
end record


Description:
A union specifies two or more variables whose storage begins at the same offset in the record. The following example sets aside two equal offsets within a record for variables of differing sizes:
begin record RecordWithUnion
  dim as long beforeUnion
  begin union
    dim as UInt8 inUnion1
    dim as Str255 inUnion2
  end union
end record

dim as RecordWithUnion myTest
myTest.inUnion2 = "COW"
print myTest.inUnion1

The variable myTest.inUnion1 is a single byte which occupies the same space as the first byte in the string myTest.inUnion2. In this case, myTest.inUnion1 happens to be the length byte of the string and the print statement will produce "3". Such an overlap is not necessary and the two values may have no relation to one another except that they start at the same location in memory.
When FutureBasic encounters a begin union statement, all dim statements up to the end union statement are examined and the largest item in the union determines the amount of space set aside by the compiler. In the example above, the union would occupy 256 bytes since the largest element in the union is a 256 byte Pascal string.

See Also:
dim; begin record