Skip to main content
Version: Nightly

ParameterList

struct ParameterList[type: AnyType, //, values: KGENParamList[type]]

A utility class to access homogeneous variadic parameters.

ParameterList is used by homogenous variadic parameter lists. Unlike VariadicPack (which is heterogeneous), ParameterList requires all elements to have the same type.

ParameterList is only used for parameter lists, VariadicList is used for function arguments.

For example, in the following function signature, *args: Int creates a ParameterList because it uses a single type Int instead of a variadic type parameter. The * before args indicates that args is a variadic argument, which means that the function can accept any number of arguments, but all arguments must have the same type Int.

def sum_values[*args: Int]() -> Int:
var total = 0

# Can use regular for loop because args is a ParameterList
for i in range(len(args)):
total += args[i] # All elements are Int, so uniform access

return total

def main():
print(sum_values[1, 2, 3, 4, 5]())

Parameters

  • type (AnyType): The type of the elements in the list.
  • values (KGENParamList): The values in the list.

Implemented traits

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

comptime members

__getitem_param__

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

Gets a single element on the variadic list.

Parameters

  • idx (Int):

empty_of

comptime empty_of[type: AnyType] = ParameterList[]

Form an empty compile-time list of values some element type.

Examples:

comptime Ints = ParameterList.empty_of[Int]()

Parameters

  • type (AnyType): The type of the elements in the list.

map_to_type

comptime map_to_type[Trait: AnyTrait[AnyType], //, Mapper: [Elt: type] Trait] = comptime[ToT: Trait, +] TypeList[#kgen.param_list.tabulate(ParameterList[values], [idx: __mlir_type.index] Mapper[values[idx]])]

Convert each element of this list into a type, forming a TypeList with the result.

Parameters

  • Trait (AnyTrait): The trait of the resulting TypeList.
  • Mapper (````): A generator that maps an element of this list to a type.

of

comptime of[type: AnyType, //, *values: type] = ParameterList[values]

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

Examples:

comptime Ints = ParameterList.of[4, 5, 6]
comptime Strings = ParameterList.of["foo", "bar", "baz"]

Parameters

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

reduce

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

Form a value by applying a function that merges each element into a starting value, then return the result.

Parameters

  • FromAndTo (AnyType): The type of the input and output result.
  • BaseVal (FromAndTo): The initial value to reduce on.
  • Reducer (````): A [BaseVal: FromAndTo, T: Self.type] -> FromAndTo that does the reduction.

size

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

The number of elements in the list.

splat

comptime splat[type: AnyType, //, count: Int, value: type] = ParameterList[#kgen.param_list.tabulate(count, [idx: __mlir_type.index] value)]

Builds a homogeneous parameter list by repeating value count times.

Parameters

  • type (AnyType): The element type.
  • count (Int): The number of copies of value in the result.
  • value (type): The value to repeat at every index.

tabulate

comptime tabulate[type: AnyType, //, count: Int, Mapper: [Idx: Int] type] = ParameterList[#kgen.param_list.tabulate(count, [idx: __mlir_type.index] Mapper[idx])]

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

Parameters

  • type (AnyType): The element type of the resulting list.
  • count (Int): The length of the result; the mapper is invoked for each index in 0..<count.
  • Mapper (````): Compile-time generator mapping Int index to a value of type.

Methods

__init__

__init__() -> Self

Constructs a ParameterList.

__getitem__

__getitem__(self, idx: Int) -> ref[StaticConstantOrigin] type

Gets a single element on the variadic list.

Args:

  • idx (Int): The index of the element to access on the list.

Returns:

ref: The element on the list corresponding to the given index.

__len__

__len__(self) -> Int

Gets the size of the list.

Returns:

Int: The number of elements on the variadic list.

get_span

static get_span() -> Span[type, StaticConstantOrigin]

Gets a span of the elements on the variadic list.

Returns:

Span: A span of the elements on the variadic list.

any_satisfies

static any_satisfies[predicate: [Elt: type] Bool]() -> Bool

'any_satisfies' applies a function to each element and returns true if the function returns True for any element.

Parameters:

  • predicate (````): A [elt: Self.Type] -> Bool comptime expression to apply.

Returns:

Bool: True if the predicate returns True for any element, False otherwise.

all_satisfies

static all_satisfies[predicate: [Elt: type] Bool]() -> Bool

'all_satisfies' applies a function to each element and returns true if the function returns True for all elements.

Parameters:

  • predicate (````): A [elt: Self.Type] -> Bool comptime expression to apply.

Returns:

Bool: True if the predicate returns True for all elements, False otherwise.

contains

static contains[value: type]() -> Bool where conforms_to(type, AnyType & ImplicitlyDestructible & Equatable)

Check if a value is contained in a variadic sequence of values.

Parameters:

  • value (type): The value to search for.

Returns:

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

write_to

write_to(self, mut writer: T)

Writes the elements of this variadic list to a writer.

Constraints:

type must conform to Writable.

Args:

  • writer (T): The object to write to.

write_repr_to

write_repr_to(self, mut writer: T)

Writes the repr of this variadic list to a writer.

Constraints:

type must conform to Writable.

Args:

  • writer (T): The object to write to.

__iter__

__iter__(ref self) -> _ParameterListIter[ParameterList()] where conforms_to(type, AnyType & Copyable & Movable)

Iterate over the list.

Returns:

_ParameterListIter: An iterator to the start of the list.