IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0
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).

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]():

# Iterate over elements at compile time
comptime for i in range(args.size): # can also use len() at comptime

# print() below is a run-time call, so placing args[i] directly inside
# it will invoke run-time access and not compile-time access; both share
# the same syntax. For illustration, we place comptime access in a separate
# step here.
comptime arg = args[i]
print(arg, end=" ")

print()

# Iterate over elements at run-time
var total = 0
for i in range(len(args)): # can also use the comptime args.size
total += args[i]

print(total)

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

# Output:
# 1 2 3 4 5
# 15

Parameters​

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

Implemented traits​

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

comptime members​

__getitem_param__​

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

Gets a single element on the variadic list.

Parameters​

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: __generator_type[Elt: type] Trait] = comptime[ToT: Trait, //] TypeList[#kgen.param_list.tabulate(len(values), [idx: __mlir_type.index] Mapper[values[SIMDLength(Int(idx))]])]

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

Parameters​

  • ​Trait (AnyTrait[AnyType]): The trait of the resulting TypeList.
  • ​Mapper (__generator_type[Elt: type] Trait): 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: __generator_type[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 (__generator_type[Prev: FromAndTo, From: type] FromAndTo): A [BaseVal: FromAndTo, T: Self.type] -> FromAndTo that does the reduction.

size​

comptime size = SIMD(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: __generator_type[Idx: Int] type] = ParameterList[#kgen.param_list.tabulate(count, [idx: __mlir_type.index] Mapper[Int(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 (__generator_type[Idx: Int] type): Compile-time generator mapping Int index to a value of type.

Methods​

__init__​

def __init__() -> Self

Constructs a ParameterList.

__getitem__​

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

Gets a single element on the variadic list.

Args:

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

Returns:

ref[ImmStaticOrigin] type: The element on the list corresponding to the given index.

__len__​

def __len__(self) -> Int

Gets the size of the list.

Returns:

Int: The number of elements on the variadic list.

get_span​

static def get_span() -> Span[type, ImmStaticOrigin]

Gets a span of the elements on the variadic list.

Returns:

Span[type, ImmStaticOrigin]: A span of the elements on the variadic list.

any_satisfies​

static def any_satisfies[predicate: __generator_type[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 (__generator_type[Elt: type] Bool): 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 def all_satisfies[predicate: __generator_type[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 (__generator_type[Elt: type] Bool): A [elt: Self.Type] -> Bool comptime expression to apply.

Returns:

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

contains​

static def contains[value: type]() -> Bool where conforms_to(type, 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​

def write_to(self, mut writer: T) where conforms_to(type, Writable)

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​

def write_repr_to(self, mut writer: T) where conforms_to(type, Writable)

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__​

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

Iterate over the list.

Returns:

_ParameterListIter[ParameterList()]: An iterator to the start of the list.