Skip to main content

Mojo struct

VariadicList

struct VariadicList[elt_is_mutable: Bool, origin: Origin[mut=elt_is_mutable], //, element_type: AnyType, is_owned: Bool]

A utility class to access variadic function arguments of memory-only types that may have ownership. It exposes references to the elements in a way that can be enumerated. Each element may be accessed with elt[].

Parameters

  • elt_is_mutable (Bool): True if the elements of the list are mutable for an mut or owned argument.
  • origin (Origin): The origin of the underlying elements.
  • element_type (AnyType): The type of the elements in the list.
  • is_owned (Bool): Whether the elements are owned by the list because they are passed as an 'var' argument.

Fields

  • value (Variadic[ref[_mlir_origin] element_type]): The underlying storage, a variadic list of references to elements of the given type.

Implemented traits

AnyType, ImplicitlyDestructible, Movable, RegisterPassable, Sized, Writable

comptime members

reference_type

comptime reference_type = Pointer[element_type, origin]

The pointer type for references to elements.

Methods

__del__

__del__(deinit self)

Destructor that releases elements if owned.

__getitem__

__getitem__[self_origin: ImmutOrigin](ref[elt_is_mutable] self, idx: Int) -> ref[origin, elt_is_mutable] element_type

Gets a single element on the variadic list.

Parameters:

  • self_origin (ImmutOrigin): The origin of the list.

Args:

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

Returns:

ref: A low-level pointer to the element on the list corresponding to the given index.

consume_elements

consume_elements[elt_handler: def(idx: Int, var elt: element_type) capturing -> None](deinit self)

Consume the variadic list by transferring ownership of each element into the provided closure one at a time. This is only valid on 'owned' variadic lists.

Parameters:

  • elt_handler (def(idx: Int, var elt: element_type) capturing -> None): A function that will be called for each element of the list.

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

element_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:

element_type must conform to Writable.

Args:

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

__iter__

__iter__[self_origin: ImmutOrigin](ref[elt_is_mutable] self) -> _VariadicListIter[element_type, origin, self_origin, is_owned]

Iterate over the list.

Parameters:

  • self_origin (ImmutOrigin): The origin of the list.

Returns:

_VariadicListIter: An iterator to the start of the list.

Was this page helpful?