generics_in_modules.adl
1 /* 2 seed = 123456789 3 min_height = 16 4 5 [case] 6 program = "test.alpha" 7 function = "main" 8 input = [] 9 */ 10 11 program test.alpha { 12 transition main() -> u32 { 13 let mat = common::use_matrix(); // Matrix with modified elements 14 15 let outer = outer::OuterWrapper::[4] { 16 nested: outer::inner::InnerGeneric::[4] { 17 data: [0; 4], 18 }, 19 }; 20 21 // ✅ Modify struct field using assignment 22 for i in 0u32..4u32 { 23 outer.nested.data[i] = i * 10; 24 } 25 26 // ✅ Modify nested matrix elements 27 for i in 0u32..common::R { 28 for j in 0u32..common::C { 29 mat.inner.values[i][j] = mat.inner.values[i][j] + 1; 30 } 31 } 32 33 return mat.inner.values[0][1] + outer.nested.data[3]; 34 } 35 36 @noupgrade 37 async constructor() {} 38 39 } 40 41 // --- Next Module: common.leo --- // 42 43 const R: u32 = 2; 44 const C: u32 = 3; 45 46 // ✅ Generic struct representing a matrix 47 struct Matrix::[M: u32, N: u32] { 48 values: [[u32; N]; M], 49 } 50 51 // ✅ Generic struct wrapping another generic struct (nested generics) 52 struct WrappedMatrix::[M: u32, N: u32] { 53 inner: Matrix::[M, N], 54 } 55 56 // ✅ Function constructing matrix and modifying it with loop/assignment 57 inline use_matrix() -> WrappedMatrix::[R, C] { 58 let base = Matrix::[R, C] { 59 values: [[1; C]; R], 60 }; 61 62 // ✅ Modify inner matrix values using loop and assignment 63 for i in 0u32..R { 64 for j in 0u32..C { 65 base.values[i][j] = i + j; 66 } 67 } 68 69 return WrappedMatrix::[R, C] { 70 inner: base, 71 }; 72 } 73 74 // --- Next Module: outer.leo --- // 75 76 struct OuterWrapper::[N: u32] { 77 nested: inner::InnerGeneric::[N], 78 } 79 80 // --- Next Module: outer/inner.leo --- // 81 82 struct InnerGeneric::[N: u32] { 83 data: [u32; N], 84 }