Skip to main content
Log in

Mojo struct

Tuple

struct Tuple[*element_types: CollectionElement]

The type of a literal tuple expression.

A tuple consists of zero or more values, separated by commas.

Parameters

  • *element_types (CollectionElement): The elements type.

Fields

  • storage (!kgen.pack<:variadic<trait<@stdlib::@builtin::@value::@CollectionElement>> element_types>): The underlying storage for the tuple.

Implemented traits

AnyType, CollectionElement, Copyable, Movable, Sized, UnknownDestructibility

Methods

__init__

__init__(out self, owned *args: *element_types)

Construct the tuple.

Args:

  • *args (*element_types): Initial values.

__init__(out self, *, owned storage: VariadicPack[origin, CollectionElement, element_types])

Construct the tuple from a low-level internal representation.

Args:

  • storage (VariadicPack[origin, CollectionElement, element_types]): The variadic pack storage to construct from.

__copyinit__

__copyinit__(out self, existing: Self)

Copy construct the tuple.

Args:

  • existing (Self): The value to copy from.

__moveinit__

__moveinit__(out self, owned existing: Self)

Move construct the tuple.

Args:

  • existing (Self): The value to move from.

__del__

__del__(owned self)

Destructor that destroys all of the elements.

__getitem__

__getitem__[idx: Int](ref self) -> ref [self] element_types[idx.value]

Get a reference to an element in the tuple.

Parameters:

  • idx (Int): The element to return.

Returns:

A reference to the specified element.

__contains__

__contains__[T: EqualityComparableCollectionElement](self, value: T) -> Bool

Return whether the tuple contains the specified value.

For example:

var t = Tuple(True, 1, 2.5)
if 1 in t:
print("t contains 1")
var t = Tuple(True, 1, 2.5)
if 1 in t:
print("t contains 1")

Parameters:

  • T (EqualityComparableCollectionElement): The type of the value.

Args:

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

Returns:

True if the value is in the tuple, False otherwise.

__len__

static __len__() -> Int

Return the number of elements in the tuple.

Returns:

The tuple length.

__len__(self) -> Int

Get the number of elements in the tuple.

Returns:

The tuple length.

get

get[i: Int, T: CollectionElement](ref self) -> ref [self] T

Get a tuple element and rebind to the specified type.

Parameters:

  • i (Int): The element index.
  • T (CollectionElement): The element type.

Returns:

The tuple element at the requested index.