Mojo function
struct_field_type_by_name
struct_field_type_by_name[StructT: AnyType, name: StringLiteral[value]]() -> ReflectedType[#kgen.struct_field_type_by_name<:trait<_std::_builtin::_anytype::_AnyType> StructT, #kgen.param.decl.ref<"name.value"> : !kgen.string>]`
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:
struct Point:
var x: Int
var y: Float64
fn 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 = 42Parameters:
- StructT (
AnyType): A concrete struct type to introspect. - name (
StringLiteral): The name of the field to look up.
Returns:
ReflectedType: A ReflectedType wrapper containing the field's type.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!