Skip to main content

Mojo struct

TypeList

struct TypeList[Trait: AnyTrait[AnyType], //, *element_types: Trait]

A compile-time list of types conforming to a common trait.

TypeList provides type-level operations on variadic sequences of types, such as reversing, filtering, slicing, mapping, and membership testing.

Examples:

from std.builtin.variadics import TypeList
from std.sys.intrinsics import _type_is_eq

# Create a type list
comptime tl = TypeList[Trait=AnyType, Int, String, Float64]()

# Query size
assert_equal(tl.size, 3)

# Check membership
comptime assert tl.contains[Int]
comptime assert not tl.contains[Bool]

# Index into the list
comptime assert _type_is_eq[tl[0], Int]()

Parameters

  • Trait (AnyTrait): The trait that all types in the list must conform to.
  • *element_types (Trait): The types in the list.

Implemented traits

AnyType, ImplicitlyDestructible, Sized

comptime members

__getitem_param__

comptime __getitem_param__[idx: Int] = element_types[idx]

Gets a type at the given index.

Parameters

  • idx (Int): The index of the type to access.

contains

comptime contains[type: Trait] = #kgen.variadic.reduce(element_types, base=False, reducer=[PrevV: Variadic[Bool], VA: Variadic[Trait], idx: __mlir_type.index] _type_is_eq_parse_time[VA[idx], type]() if _type_is_eq_parse_time[VA[idx], type]() else PrevV[0])[0]

Checks if a type is contained in this type list.

Parameters

  • type (Trait): The type to check for.

filter

comptime filter[predicate: [Type: Trait] Bool] = TypeList[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[Trait], VA: Variadic[Trait], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx]) if predicate[VA[idx]] else PrevV)]

Filters types based on a predicate.

Returns a new variadic containing only the types for which the predicate returns True.

Parameters

  • predicate (````): A generator that takes a type and returns Bool.

map

comptime map[To: AnyTrait[AnyType], //, Mapper: [From: Trait] To] = TypeList[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[To], VA: Variadic[Trait], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, Mapper[VA[idx]]))]

Maps types to new types using a mapper.

Returns a new variadic of types resulting from applying Mapper[T] to each type in this type list.

Parameters

  • To (AnyTrait): The trait that the output types conform to.
  • Mapper (````): A generator that maps a type to another type. The generator type is [T: Trait] -> To.

reverse

comptime reverse = TypeList[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[Trait], VA: Variadic[Trait], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[(add (mul idx, -1), len(VA), -1)]))]

The types in reverse order.

size

comptime size = Int(mlir_value=len(element_types))

The number of types in the list.

slice

comptime slice[start: Int where (start >= 0) = 0, end: Int where (end <= TypeList[element_types].size) if (start <= end) else (start <= end) = TypeList[element_types].size] = TypeList[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[Trait], VA: Variadic[Trait], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[idx]) if (idx < end) if (idx >= start) else (idx >= start) else PrevV)]

Extracts a contiguous subsequence from the type list.

Returns a new variadic containing elements from index start (inclusive) to index end (exclusive). Similar to Python's slice notation [start:end].

Constraints: 0 <= start <= end <= size.

Parameters

  • start (Int): The starting index (inclusive). Defaults to 0.
  • end (Int): The ending index (exclusive). Defaults to the list size.

Methods

__len__

__len__(self) -> Int

Gets the size of the TypeList.

Returns:

Int: The number of elements on the TypeList.

Was this page helpful?