Mojo function
struct_field_names
struct_field_names[T: AnyType]() -> InlineArray[StaticString, struct_field_count[T]()]
Returns the names of all fields in struct T as an InlineArray.
This function works with both concrete types and generic type parameters.
Note: For best performance, assign the result to a comptime variable to
ensure compile-time evaluation:
comptime names = struct_field_names[T]()
Example:
fn print_field_names[T: AnyType]():
comptime names = struct_field_names[T]()
@parameter
for i in range(struct_field_count[T]()):
print(names[i])
fn main():
print_field_names[MyStruct]() # Works with any struct!Constraints:
T must be a struct type. Passing a non-struct type results in a compile-time error.
Parameters:
- T (
AnyType): A struct type.
Returns:
InlineArray: An InlineArray of StaticStrings, one for each field name in the struct.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!