Declares a user-defined type.
Type is used to declare custom data types containing one or more data fields, including integer types, floating point types, fixed-size or variable-length (dynamic) arrays, fixed-size or variable-length strings, bitfields, or other user-defined types.
Types support various functionality related to object-oriented programming:
A
Type can also contain nested types or unions, allowing data members to be grouped as desired.
Type and
Union can be directly nested on condition of alternating their nesting.
Only the main structure (Type or Union) can be named, the others (nested) must be unnamed.
Nested unnamed
Type or
Union can not have procedure members or static data members (same restriction for local scope named Type/Union).
Alias "alternatename" specifies that if
typename must be encoded (mangled) in to a public symbol (as in an object module or library), then specifically use
alternate name instead of the usual encoding (mangling) of
typename.
Memory layout
Types lay out their fields consecutively in memory, following the native alignment and padding rules (described on the
Field page). Special care must be taken when using Types for file I/O or interacting with other programs or programming languages, in case the alignment and padding rules are different. The optional
Field = number specifier can be used to change the behavior on the FreeBASIC side.
Variable-length data
In FreeBASIC, Type data structures must ultimately be fixed-size, such that the compiler knows how much memory to allocate for objects of that Type. Nevertheless, Types may contain variable-length (dynamic) string or array data members. However, the string's/array's data will not be embedded in the Type directly. Instead, the Type will only contain a
String/array descriptor structure, which FreeBASIC uses behind the scenes to manage the variable-length string/array data. For sizing the structure of the array descriptor in the Type, a variable-length (dynamic) array data member must be always declared by using
Any(S) in place of the array bounds, in order to fix the amount of dimensions based on the number of Anys specified. A variable-length (dynamic) array data member can also be pre-sized in its declaration by using syntax with
ReDim.
Variable-length array fields are considered as pseudo-objects when they are declared in a
Type, just like variable-length strings (the implicit copy constructor and the implicit let operator themselves support [re]sizing and copying such arrays, or their erasing).
Because of that, saving such a Type into a file will write out the descriptor, not the actual string/array data. In order to embed strings/arrays into Types directly, fixed-length strings/arrays must be used.
Similarly, when maintaining dynamic data manually through the use of pointers within a Type, it does usually not make sense to save the Type to a file, because the address stored in the pointer field will be written to file, not the actual memory it points to. Addresses are meaningful to a specific process only though, and cannot be shared that way.
Special note on fixed-length strings
Currently, fixed-length string fields of
String * N type have an extra null terminator at their end, for compatibility with C strings, making them incompatible with QB strings inside Types, because they actually use up
N+1 bytes, instead of just
N bytes. A possible work-around is to declare the field
As String * (N-1), though this will not work in future releases if the null terminator is removed. Another alternative is to use a
Byte or
UByte array with the proper size.
Note on bitfields ( fieldname
: bits
)
Bitfields can only be declared inside a type or a union, and allow to specify some very small objects of a given number of bits in length. Each field is accessed and manipulated as if it were an ordinary member of the structure.
Only integer data-types (up to 32-bit for 32-bit development or 64-bit for 64-bit development) are valid. The sizes of the declared data-types, large enough to contain the bit patterns, affect how the bitfields are placed in memory.
Bitfield members in a type are packed together, unless the next member is a non-bitfield (nested union is considered a non-bitfield).
A bitfield does not have any address (one cannot get a pointer to it and its offset inside the structure).