Lately I've been working with records that should have a variable-type content and it drives me crazy when I have to declare such structures. Can Free Pascal finally make readable unions instead of forcing the use of variant-record-abomination? Their syntax is so idiotic that it is hard to believe that someone who has seen Pascal at least once in their life was responsible for their design.
Finally, I suggest that unions be implemented legibly, so that they can be conveniently declared and matched to the structures used in the C language. There must be a normal, Pascal syntax, the ability to create named fields (of any type) and the ability to declare fields under a field that is a union (which is impossible with variant-records, without declaring additional structures).
Suggested syntax:
type
TFoo = union
// union fields
end;
The same as in the case of records. All fields of such a union occupy the same memory area. For example:
type
TFoo = union
Letter: Char; // TFoo.Letter
Code: UInt8; // TFoo.Code
end;
The size of such a union is
one byte. Unions can have fields of simple types, but also arrays, structures, and other unions. A more complex example to illustrate nesting:
type
TFoo = union
Chunks: union // TFoo.Chunks
Number: UInt32; // TFoo.Chunks.Number
Words: array [0 .. 1] of UInt16; // TFoo.Chunks.Words
Bytes: union // TFoo.Chunks.Bytes
Unsigned: array [0 .. 3] of UInt8; // TFoo.Chunks.Bytes.Unsigned
Signed: array [0 .. 3] of Int8; // TFoo.Chunks.Bytes.Signed
end;
end;
Other: UInt32; // TFoo.Other
end;
The size of
TFoo union is
8 bytes —
Chunks occupies 4 bytes,
Other occupies 4 bytes (is below the union). This syntax is not only readable and consistent with the syntax of other Pascal blocks (explicit termination of the union body, including a nested union), but also allows fields to be declared as unions, and subsequent fields (including structures and unions) below them. You don't need to declare complex field types outside of the main union body. Thus, even very complex unions containing many fields, including nested unions and structures, can be declared as a single and readable data type.
I am asking FPC developers to consider implementing support for such unions to say goodbye to stupid variant-records once and for all. By the way — I don't know why, but the snippet syntax highlighter recognizes the word
union and colors it like Pascal's keywords. Good sign.
