Skip to main content

Mojo struct

VariadicParamList

@register_passable(trivial) struct VariadicParamList[type: TrivialRegisterPassable]

A utility class to access homogeneous variadic parameters.

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

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

For example, in the following function signature, *args: Int creates a VariadicParamList 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.

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

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

    return total

fn main():
    print(sum_values(1, 2, 3, 4, 5))

Parameters

Fields

  • value (Variadic[type]): The underlying storage for the variadic list.

Implemented traits

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

comptime members

__copy_ctor_is_trivial

comptime __copy_ctor_is_trivial = True

__del__is_trivial

comptime __del__is_trivial = True

__move_ctor_is_trivial

comptime __move_ctor_is_trivial = True

IteratorType

comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = _VariadicParamListIter[type]

The iterator type for this variadic list.

Parameters

  • iterable_mut (Bool): Whether the iterable is mutable.
  • iterable_origin (Origin): The origin of the iterable.

Methods

__init__

__init__(*values: type, *, comptime_only: Tuple[]) -> Self

Constructs a VariadicParamList from a variadic list of values at comptime. NOTE: this initializer only works at comptime.

Args:

  • *values (type): The variadic argument list to construct the variadic list with.
  • comptime_only (Tuple): Tell callers that this only works at comptime.

__init__(values: VariadicList[type, False], comptime_only: Tuple[]) -> Self

Constructs a VariadicParamList from a variadic list of values at comptime. NOTE: this initializer only works at comptime.

Args:

  • values (VariadicList): The variadic argument list to construct the variadic list with.
  • comptime_only (Tuple): Tell callers that this only works at comptime.

__getitem__

__getitem__(self, idx: Int) -> type

Gets a single element on the variadic list.

Args:

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

Returns:

type: 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.

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) -> _VariadicParamListIter[type]

Iterate over the list.

Returns:

_VariadicParamListIter: An iterator to the start of the list.

Was this page helpful?