Skip to main content
Version: Nightly

struct_field_names

struct_field_names[T: AnyType]() -> InlineArray[StringSlice[StaticConstantOrigin], 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:

from std.reflection import struct_field_names, struct_field_count

struct MyStruct:
var x: Int
var y: Float64

def print_field_names[T: AnyType]():
comptime names = struct_field_names[T]()
comptime for i in range(struct_field_count[T]()):
print(names[i])

def 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:

Returns:

InlineArray: An InlineArray of StaticStrings, one for each field name in the struct.