Skip to main content

Mojo struct

ParameterList

struct ParameterList[type: AnyType, //, *values: 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 (type): 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):

size

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

The number of elements in the list.

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.

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[rebind[Variadic[type(Copyable)]](values)] where conforms_to(type, AnyType & Copyable & Movable)

Iterate over the list.

Returns:

_ParameterListIter: An iterator to the start of the list.

Was this page helpful?