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_type_by_name
struct_field_type_by_name[StructT: AnyType, name: StringLiteral[name.value]]() -> Reflected[#kgen.struct_field_type_by_name<:trait<_std::_builtin::_anytype::_AnyType> StructT, #kgen.param.decl.ref<"name.value`"> : !kgen.string>]
Deprecated: use reflect[StructT]().field_type[name]() instead.
Returns the type of the field with the given name in struct StructT.
This function provides compile-time lookup of a struct field's type by name. It produces a compile error if the field name does not exist in the struct.
The returned ReflectedType wrapper contains the field type as its T
parameter, which can be used as a type in declarations.
Note: StructT must be a concrete type, not a generic type parameter.
See the module documentation for details on this limitation.
Example:
from std.reflection import struct_field_type_by_name
struct Point:
var x: Int
var y: Float64
def example():
# Get the type of field "x"
comptime x_type = struct_field_type_by_name[Point, "x"]()
# x_type.T is Int
var value: x_type.T = 42
Deprecated: Use reflect[StructT]().field_type[name]() instead. The returned Reflected[FieldT] exposes the field type via its T parameter.
Parameters:
- StructT (
AnyType): A concrete struct type to introspect. - name (
StringLiteral[name.value]): The name of the field to look up.
Returns:
Reflected[#kgen.struct_field_type_by_name<:trait<_std::_builtin::_anytype::_AnyType> StructT, #kgen.param.decl.ref<"name.value`"> : !kgen.string>]: A ReflectedType wrapper containing the field's type.