Struct Statement

Makes a simple compound data type.

Syntax

Struct struct_name [ type_list ] [ Is base_type ]
    [ Where generic_constraints ]
    …
    element_name As element_type
    …
    [ statements ]
End [ Struct ]

Parts

struct_name
Mandatory name for the structure.
type_list
Optional. See Type List for more information.
base_type
Optional value type: an elementary data type (Int for example) or the name of a different structure.
Where
Optional keyword that you can use again and again. See Where Clause (Generics) for more information.
element_name
Mandatory name for an element of the structure.
element_type
Mandatory value type: an elementary data type or the name of a different structure.
statements
Optional procedure declarations.
End
Completes the statement. You can also use End Struct.
You can change this part of the syntax. See @Option Directive for more information.

Instructions

Struct is short for “structure”.

You can put a structure only in some locations. These include modules (Program and Library), and declarations of Class, Object, and Trait. You cannot put a structure in a procedure. See Declaration Contexts and Default Access Levels for more information.

Permitted members

Bit fields

A bit field is an integer member with a width in bits specified in code. You use the data types SBits and UBits only in a structure. You must supply the width between parentheses immediately after SBits or UBits. The width must be an integer literal in the range 1 thru 128.

UBits is short for “unsigned bit field”.

SBits is short for “signed bit field”. When you use SBits, the most significant bit is reserved as a sign bit.

A base type is most important when used with bit fields. When you supply a base type, you set the width of the structure. If the width of the members put together is more than the width of the base type, then this is an error. If the width of the members is less than the base type, then there are bits that are not used, known as padding.

If a structure mixes bit fields and named types, then the named types will usually be put in boundaries. The type of CPU sets the boundaries, counted from the start of the Struct: Intel processors usually have 8-bit boundaries, while Motorola processors usually have 16-bit boundaries. A bit field must not go across a boundary.

Examples

Struct point
    x As Real64
    y As Real64
End Struct

See also