## 7. Data Structures ### 7.1 Structs Structs define composite types with named fields. **Definition Syntax**: `(field_types -- ) { field_names: } ::name? struct` **Examples**: ``` // Define Point struct - generic over coordinate types (T T --) { x: y: } ::Point struct // Use with specific types 3.0 4.0 Point // Creates Point with f64 fields 3 4 Point // Creates Point with i64 fields // More complex struct (T U V --) { width: height: depth: } ::Box3D struct 10.0 20.0 30.0 Box3D ``` **Field Access**: - **get**: `struct ::field get` - Consumes struct and field identifier, returns field value - **set**: `value ::field set` - Consumes value and field identifier, returns modified struct **Examples**: ``` point ::x get // Get x field (consumes point and ::x) 15.0 ::x set // Set x field to 15.0 (consumes value and ::x) // Chaining with duplication point dup ::x get 2 * over ::y get + // (point.x * 2) + point.y ``` > **Note**: Structs can be generic. See Section 10.3 for details on generic data structures. ### 7.2 Unions Unions define tagged variants, where a value is one of several possible types. **Definition Syntax**: `(variant_types -- ) { variants } ::name? union` **Examples**: ``` // Option type - generic over T (T --) { Some(T) None } ::Option union // Result type - generic over T and E (T E --) { Ok(T) Err(E) } ::Result union // Create union values 42 Option::Some // Creates Option::Some(42) Option::None // Creates Option::None "success" Result::Ok // Creates Result::Ok("success") "error" Result::Err // Creates Result::Err("error") ``` **Pattern Matching**: Unions are typically used with pattern matching (Section 6.5) to handle different variants. > **Note**: Unions can be generic. See Section 10.3 for details on generic data structures. ### 7.3 Enums Enums define a fixed set of named values with optional integer assignments. **Definition Syntax**: `{ variants } ::name enum` **Examples**: ``` { Pending 1: // Explicitly set to 1 Active: // Defaults to 2 (one plus the last) Complete 0: // Explicitly set to 0 } ::Status enum // Usage Status::Pending // Creates Status::Pending (value 1) Status::Active // Creates Status::Active (value 2) Status::Complete // Creates Status::Complete (value 0) ``` **Automatic Values**: If not specified, enum values start at 0 and increment by 1 for each subsequent variant. ### 7.4 Arrays Arrays are homogeneous collections of values. **Array Literals**: ``` [1 2 3 4 5] // array of i64 [1.0 2.0 3.0] // array of f64 [[1 2] [3 4]] // 2D array ``` **Basic Operations**: ``` arr 2 at // ( array index -- element ) Access element at index arr 1 3 slice // ( array start end -- subarray ) Extract slice arr length // ( array -- length ) Get array length ``` **Array Combinators**: These operations take TokenString arguments containing function bodies: ``` // Map - transform each element [1 2 3 4] { 2 * } map // => [2 4 6 8] // Filter - keep elements matching predicate [1 2 3 4 5] { 2 % 0 == } filter // => [2 4] // Reduce - fold with accumulator [1 2 3 4] 0 { + } reduce // => 10 // Each - apply to each element (side effects) [[1 2] [3 4]] { sum print } each ``` **Array Arithmetic**: ``` [1 2 3] [4 5 6] +. // Element-wise add: [5 7 9] [1 2 3] [4 5 6] *. // Element-wise multiply: [4 10 18] [1 2 3] 2 *. // Scalar multiply: [2 4 6] ``` **Array Manipulation**: ``` [1 2 3] [4 5 6] concat // Concatenate: [1 2 3 4 5 6] [1 2 3] reverse // Reverse: [3 2 1] [[1 2] [3 4]] transpose // Transpose: [[1 3] [2 4]] [1 2 3 4] 2 window // Sliding window: [[1 2] [2 3] [3 4]] ``` > **Implementation Details**: Array operations implement various traits including `::ArrayOf`, `::Selectable`, `::Sliceable`, and `::Sized`. See Appendix B for complete trait definitions and Appendix A for the full array operation reference. ---