Skip to main content

Mojo struct

Tuple

struct Tuple[*element_types: Movable]

The type of a literal tuple expression.

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

Parameters

  • *element_types (Movable): The elements type.

Implemented traits

AnyType, Copyable, Equatable, Hashable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, Sized, Writable

Methods

__init__

__init__() -> Tuple[]

Construct an empty tuple.

Returns:

Tuple

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

Construct the tuple.

Args:

  • *args (*element_types): Initial values.

__init__(out self, *, var storage: VariadicPack[storage.is_owned, Movable, element_types])

Construct the tuple from a low-level internal representation.

Args:

  • storage (VariadicPack): The variadic pack storage to construct from.

__init__(out self, *, copy: Self)

Copy construct the tuple.

Args:

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

__init__(out self, *, deinit take: Self)

Move construct the tuple.

Args:

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

__init__[*elt_types: Movable & Defaultable](out self: Tuple[elt_types])

Construct a tuple with default-initialized elements.

Parameters:

  • *elt_types (Movable & Defaultable): The types of the elements contained in the Tuple.

Returns:

Tuple

__del__

__del__(deinit self)

Destructor that destroys all of the elements.

__lt__

__lt__[self_elt_types: Variadic[Movable & Comparable], other_elt_types: Variadic[Movable & Comparable], //](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple using less than comparison.

Parameters:

  • self_elt_types (Variadic & Comparable]): The types of the elements contained in the Tuple.
  • other_elt_types (Variadic & Comparable]): The types of the elements contained in the other Tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if this tuple is less than the other tuple, False otherwise.

__le__

__le__[self_elt_types: Variadic[Movable & Comparable], other_elt_types: Variadic[Movable & Comparable], //](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple using less than or equal to comparison.

Parameters:

  • self_elt_types (Variadic & Comparable]): The types of the elements contained in the Tuple.
  • other_elt_types (Variadic & Comparable]): The types of the elements contained in the other Tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if this tuple is less than or equal to the other tuple, False otherwise.

__eq__

__eq__(self, other: Self) -> Bool where #kgen.variadic.reduce(element_types, base=True, reducer=[PrevV: Variadic[Bool], VA: Variadic[AnyType], idx: __mlir_type.index] PrevV[0] if conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Equatable) else conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Equatable))[0]

Compare this tuple to another tuple using equality comparison.

Args:

  • other (Self): The other tuple to compare against.

Returns:

Bool: True if this tuple is equal to the other tuple, False otherwise.

__eq__[self_elt_types: Variadic[Movable & Equatable], other_elt_types: Variadic[Movable & Equatable]](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple of a potentially different type.

This overload enables cross-type and cross-length comparisons such as (1, "a") == (1, "b") or (1, 2, 3) == (1, 2).

Parameters:

  • self_elt_types (Variadic & Equatable]): The types of the elements in this tuple.
  • other_elt_types (Variadic & Equatable]): The types of the elements in the other tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if the tuples are equal, False otherwise.

__ne__

__ne__[self_elt_types: Variadic[Movable & Equatable], other_elt_types: Variadic[Movable & Equatable]](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple of a potentially different type.

Parameters:

  • self_elt_types (Variadic & Equatable]): The types of the elements in this tuple.
  • other_elt_types (Variadic & Equatable]): The types of the elements in the other tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if the tuples are not equal, False otherwise.

__gt__

__gt__[self_elt_types: Variadic[Movable & Comparable], other_elt_types: Variadic[Movable & Comparable], //](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple using greater than comparison.

Parameters:

  • self_elt_types (Variadic & Comparable]): The types of the elements contained in the Tuple.
  • other_elt_types (Variadic & Comparable]): The types of the elements contained in the other Tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if this tuple is greater than the other tuple, False otherwise.

__ge__

__ge__[self_elt_types: Variadic[Movable & Comparable], other_elt_types: Variadic[Movable & Comparable], //](self: Tuple[self_elt_types], other: Tuple[other_elt_types]) -> Bool

Compare this tuple to another tuple using greater than or equal to comparison.

Parameters:

  • self_elt_types (Variadic & Comparable]): The types of the elements contained in the Tuple.
  • other_elt_types (Variadic & Comparable]): The types of the elements contained in the other Tuple.

Args:

  • other (Tuple): The other tuple to compare against.

Returns:

Bool: True if this tuple is greater than or equal to the other tuple, False otherwise.

__contains__

__contains__[T: Equatable](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")

Parameters:

Args:

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

Returns:

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

__len__

static __len__() -> Int

Return the number of elements in the tuple.

Returns:

Int: The tuple length.

__len__(self) -> Int

Get the number of elements in the tuple.

Returns:

Int: The tuple length.

__getitem_param__

__getitem_param__[idx: Int](ref self) -> ref[self_is_mut] element_types[idx]

Get a reference to an element in the tuple.

Parameters:

  • idx (Int): The element to return.

Returns:

ref: A reference to the specified element.

__hash__

__hash__[H: Hasher](self, mut hasher: H) where #kgen.variadic.reduce(element_types, base=True, reducer=[PrevV: Variadic[Bool], VA: Variadic[AnyType], idx: __mlir_type.index] PrevV[0] if conforms_to(VA[idx], AnyType & Hashable) else conforms_to(VA[idx], AnyType & Hashable))[0]

Hashes the tuple using the given hasher.

Parameters:

  • H (Hasher): The hasher type.

Args:

  • hasher (H): The hasher instance.

write_to

write_to(self, mut writer: T) where #kgen.variadic.reduce(element_types, base=True, reducer=[PrevV: Variadic[Bool], VA: Variadic[AnyType], idx: __mlir_type.index] PrevV[0] if conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Writable) else conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Writable))[0]

Write this tuple's text representation to a writer.

Elements are formatted using their write_to() representation. Single-element tuples include a trailing comma: (1,).

Args:

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

write_repr_to

write_repr_to(self, mut writer: T) where #kgen.variadic.reduce(element_types, base=True, reducer=[PrevV: Variadic[Bool], VA: Variadic[AnyType], idx: __mlir_type.index] PrevV[0] if conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Writable) else conforms_to(VA[idx], AnyType & ImplicitlyDestructible & Writable))[0]

Write this tuple's debug representation to a writer.

Outputs the type name and parameters followed by elements formatted using their write_repr_to() representation. For example, Tuple[Int, String](Int(0), 'hello').

Args:

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

reverse

reverse(deinit self, out result: Tuple[#kgen.variadic.reduce(element_types, base=, reducer=[PrevV: Variadic[Movable], VA: Variadic[Movable], idx: __mlir_type.index] #kgen.variadic.concat(PrevV, VA[(add (mul idx, -1), len(VA), -1)]))])

Return a new tuple with the elements in reverse order.

Usage:

image_coords = Tuple[Int, Int](100, 200) # row-major indexing
screen_coords = image_coords.reverse() # (col, row) for x,y display
print(screen_coords[0], screen_coords[1]) # output: 200, 100

Returns:

Tuple: A new tuple with the elements in reverse order.

concat

concat[*other_element_types: Movable](deinit self, deinit other: Tuple[other_element_types], out result: Tuple[#kgen.variadic.concat(element_types, other_element_types)])

Return a new tuple that concatenates this tuple with another.

Usage:

var rgb = Tuple[Int, Int, Int](0xFF, 0xF0, 0x0)
var rgba = rgb.concat(Tuple[Int](0xFF)) # Adds alpha channel
print(rgba[0], rgba[1], rgba[2], rgba[3]) # 255 240 0 255

Parameters:

  • *other_element_types (Movable): The types of the elements contained in the other Tuple.

Args:

  • other (Tuple): The other tuple to concatenate.

Returns:

Tuple: A new tuple with the concatenated elements.

Was this page helpful?