IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

struct_field_count

struct_field_count[T: AnyType]() -> Int

Deprecated: use reflect[T]().field_count() instead.

Returns the number of fields in struct T.

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 count = struct_field_count[T]()

Example:

from std.reflection import struct_field_count

struct MyStruct:
var x: Int
var y: Float64

def count_fields[T: AnyType]() -> Int:
return struct_field_count[T]()

def main():
print(count_fields[MyStruct]()) # Prints field count

Deprecated: Use reflect[T]().field_count() instead.

Constraints:

T must be a struct type. Passing a non-struct type results in a compile-time error.

Parameters:

Returns:

Int: The number of fields in the struct.