Skip to main content
Version: Nightly

TypeList

struct TypeList[Trait: AnyTrait[AnyType], //, values: KGENParamList[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, slicing, mapping, and membership testing.

Examples:

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

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

def main():
# 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.
  • values (KGENParamList): The types in the list.

Implemented traits

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, Sized, TrivialRegisterPassable

comptime members

__getitem_param__

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

Gets a type at the given index.

Parameters

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

filter_idx

comptime filter_idx[Predicate: [Elt: Trait, Idx: Int] Bool] = TypeList[#kgen.param_list.concat(#kgen.param_list.tabulate(TypeList[values], [idx: __mlir_type.index] values[idx] if Predicate[values[idx], idx] else ))]

Returns a new TypeList containing only elements selected by a predicate.

The predicate is evaluated at compile time for each (element, index) pair. Indices are the positions in this list, from 0 through size - 1.

Returns: A TypeList of the same trait containing the kept elements in order.

Parameters

  • Predicate (````): A compile-time generator [element: Self.Trait, idx: Int] -> Bool. When it returns True, element is kept in order; when False, the element is dropped.

map

comptime map[ToTrait: AnyTrait[AnyType], //, Mapper: [From: Trait] ToTrait] = comptime[ToT: ToTrait, +] TypeList[#kgen.param_list.tabulate(TypeList[values], [idx: __mlir_type.index] Mapper[values[idx]])]

Maps types to new types using a mapper.

Returns a new TypeList resulting from applying Mapper[T] to each element in this list.

Parameters

  • ToTrait (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.

map_to_values

comptime map_to_values[ValueType: AnyType, //, Mapper: [Elt: Trait] ValueType] = ParameterList[#kgen.param_list.tabulate(TypeList[values], [idx: __mlir_type.index] Mapper[values[idx]])]

Convert each type in this list to a value, forming a ParameterList.

This is the value analogue of ParameterList.map_to_type: each element type is passed to Mapper, and the resulting values share the homogeneous element type ValueType.

Parameters

  • ValueType (AnyType): The element type of the resulting ParameterList.
  • Mapper (````): A compile-time generator that maps an element type to a value. The generator type is [T: Self.Trait] -> ValueType.

of

comptime of[Trait: AnyTrait[AnyType], //, *values: Trait] = TypeList[values]

Form a compile-time list of types with some elements, uninstantiated.

Examples:

comptime Ts = TypeList.of[Trait=AnyType, Int, String, Float64, Bool]
comptime Ms = TypeList.of[Trait=Movable, Int, String, Float64, Bool]

Parameters

  • Trait (AnyTrait): The type of the elements in the list.
  • *values (Trait): The values in the list.

reduce

comptime reduce[FromAndTo: AnyType, //, BaseVal: FromAndTo, Reducer: [Prev: FromAndTo, From: Trait] FromAndTo] = #kgen.param_list.reduce(values, base=BaseVal, reducer=[PrevV: FromAndTo, VA: KGENParamList[Trait], idx: __mlir_type.index] Reducer[PrevV, VA[idx]])

Folds this type list to a single value using an associative step function.

Parameters

  • FromAndTo (AnyType): The type of the accumulator and the final result.
  • BaseVal (FromAndTo): The initial accumulator value.
  • Reducer (````): A compile-time generator [prev: FromAndTo, element: Self.Trait] -> FromAndTo.

reverse

comptime reverse[ToT: Trait, //] = TypeList[#kgen.param_list.tabulate(TypeList[values], [idx: __mlir_type.index] values[((TypeList[values].size - 1) - idx)])]

Returns this type list in reverse order.

Parameters

  • ToT (Trait):

size

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

The number of types in the list.

slice

comptime slice[start: Int = 0, end: Int = TypeList[values].size] = comptime[ToT: Trait, +] TypeList[#kgen.param_list.tabulate(max((end - start), 0), [idx: __mlir_type.index] values[(start + idx)])]

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.

splat

comptime splat[Trait: AnyTrait[AnyType], //, count: Int, type: Trait] = comptime[ToT: Trait, +] TypeList[#kgen.param_list.tabulate(count, [idx: __mlir_type.index] type)]

Splats a type a given number of times.

Parameters

  • Trait (AnyTrait): The trait that the types conform to.
  • count (Int): The number of times to splat the type.
  • type (Trait): The type to splat.

tabulate

comptime tabulate[Trait: AnyTrait[AnyType], ToT: Trait, //, count: Int, Mapper: [Idx: Int] Trait] = TypeList[#kgen.param_list.tabulate(count, [idx: __mlir_type.index] Mapper[idx])]

Builds a type list by applying an index-to-type mapper count times.

Parameters

  • Trait (AnyTrait): The trait of the generated TypeList.
  • ToT (Trait): The type of the values in the generated TypeList.
  • count (Int): The number of times to apply the generator, the length of the result..
  • Mapper (````): The generator to apply, mapping from Int to ToT.

upcast

comptime upcast[dst_trait: AnyTrait[AnyType]] = TypeList[values]

Upcasts a TypeList to a base trait.

Returns: A new TypeList with the types downcasted to the base trait.

Parameters

  • dst_trait (AnyTrait): The trait to downcast to.

Methods

__init__

__init__() -> Self

Constructs a TypeList.

any_satisfies

static any_satisfies[predicate: [Type: Trait] Bool]() -> Bool

Returns true if predicate holds for at least one type in this list.

Parameters:

  • predicate (````): A compile-time generator [T: Self.Trait] -> Bool.

Returns:

Bool: True if predicate holds for at least one type in this list, False otherwise.

all_satisfies

static all_satisfies[predicate: [Type: Trait] Bool]() -> Bool

Returns true if predicate holds for every type in this list.

For an empty list, returns true.

Parameters:

  • predicate (````): A compile-time generator [T: Self.Trait] -> Bool.

Returns:

Bool: True if predicate holds for every type in this list, False otherwise.

contains

static contains[type: Trait]() -> Bool

Checks if a type is contained in this type list.

Parameters:

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

Returns:

Bool: True if the type is contained in this type list, False otherwise.

__len__

__len__(self) -> Int

Gets the size of the TypeList.

Returns:

Int: The number of elements on the TypeList.