Structures
Structs in Helix
Structs in HelixIn Helix, structs are used exclusively for storing aggregate types. Unlike classes, structs cannot have methods, constructors, or derived relationships. However, they can:
- Contain other structs.
- Contain variables (fields).
- Contain Type Aliases/Definitions.
- Contain anonymous or named enumerations.
- Overload anonymous operators (
op
without aliases). - Use interfaces with the
with
keyword to implement variables and operators.
Structs are a lightweight and efficient way to organize data that does not require the additional functionality of classes.
Declaring Structs
Declaring StructsTo declare a struct, use the struct
keyword followed by the name of the struct and its fields.
Creating Struct Instances
Creating Struct InstancesStructs do not have constructors. Instead, you initialize them directly by assigning values to their fields:
- Implicit Object Initialization:
{ .field1 = value1, .field2 = value2 }
syntax. This syntax does requires the type to be specified.
- Explicit Object Initialization: Using the
Point { x = 10, y = 20 }
syntax. This syntax does not require the type to be specified.
Using requires
with Structs
Using requires with StructsStructs can define generic parameters using the requires
keyword. This allows you to create structs that can store or manipulate data types generically.
Syntax Overview
Syntax OverviewThe requires
declaration specifies type parameters and optionally enforces constraints on those parameters. Constraints are covered in more detail in Requires.
Example: Struct with Requires
Example: Struct with RequiresStructs with
Interfaces
Structs with InterfacesStructs can implement variables and anonymous operators (op
) from interfaces using the with
keyword. However:
- Only variables and operators are supported.
- Functions in interfaces cannot be implemented by structs.
Example: Struct with Interface
Example: Struct with InterfaceOperator Overloading
Operator OverloadingStructs can overload anonymous operators (operators without an alias). This allows intuitive usage such as a + b
instead of explicit function calls.
Example: Overloading Arithmetic Operators
Example: Overloading Arithmetic OperatorsOverloading the Cast Operator (as
)
Overloading the Cast Operator (as)Structs can define custom casting behavior with the as
operator. This allows explicit conversions between types.
Example: Custom Cast Operator
Example: Custom Cast OperatorExtending Structs
Extending StructsStructs can be extended to add new fields or operators. This allows you to expand the functionality of existing structs without modifying their original definition.
Example: Extending a Struct
Example: Extending a StructBest Practices for Structs
Best Practices for StructsConclusion
ConclusionStructs in Helix are lightweight and efficient for storing aggregate data. By restricting their functionality to fields and anonymous operators, Helix ensures that structs remain simple and focused on their purpose.