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,
Defaultable,
Equatable,
Hashable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable,
RegisterPassable,
Sized,
Writable
Methods
__init__
__init__(out self)
Construct a tuple with default-initialized elements.
Constraints:
All element_types must conform to Defaultable. The constraint
is enforced via a per-element comptime assert in the body
instead of an explicit where clause so that callers whose
element types come from a comptime reducer (which the solver
can't reduce through when checking AllDefaultable[...]) can
still default-construct.
__init__(out self, var *args: *element_types.values)
Construct the tuple.
Args:
- *args (
*element_types.values): Initial values.
__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.
__del__
__del__(deinit self)
Destructor that destroys all of the elements.
__lt__
__lt__[elt_types: KGENParamList[Movable & Comparable], //](self: Tuple[*elt_types], other: Tuple[*elt_types]) -> Bool
Compare this tuple to another tuple using less than comparison.
Parameters:
- elt_types (
KGENParamList&Comparable]): The types of the elements contained in the 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__[elt_types: KGENParamList[Movable & Comparable], //](self: Tuple[*elt_types], other: Tuple[*elt_types]) -> Bool
Compare this tuple to another tuple using less than or equal to comparison.
Parameters:
- elt_types (
KGENParamList&Comparable]): The types of the elements contained in the 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 TypeList.all_satisfies[comptime[Type: AnyType] conforms_to(Type, AnyType & ImplicitlyDestructible & Equatable)]()
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.
__gt__
__gt__[elt_types: KGENParamList[Movable & Comparable], //](self: Tuple[*elt_types], other: Tuple[*elt_types]) -> Bool
Compare this tuple to another tuple using greater than comparison.
Parameters:
- elt_types (
KGENParamList&Comparable]): The types of the elements contained in the 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__[elt_types: KGENParamList[Movable & Comparable], //](self: Tuple[*elt_types], other: Tuple[*elt_types]) -> Bool
Compare this tuple to another tuple using greater than or equal to comparison.
Parameters:
- elt_types (
KGENParamList&Comparable]): The types of the elements contained in the 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:
- T (
Equatable): The type of the value.
Args:
- value (
T): The value to search for.
Returns:
Bool: True if the value is in the tuple, False otherwise.
__len__
__getitem_param__
__getitem_param__[idx: Int](ref self) -> ref[idx] element_types.values[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 TypeList.all_satisfies[comptime[Type: AnyType] conforms_to(Type, AnyType & Hashable)]()
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 TypeList.all_satisfies[comptime[Type: AnyType] conforms_to(Type, AnyType & ImplicitlyDestructible & Writable)]()
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 TypeList.all_satisfies[comptime[Type: AnyType] conforms_to(Type, AnyType & ImplicitlyDestructible & Writable)]()
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.param_list.tabulate(TypeList[element_types.values], [idx: __mlir_type.index] element_types.values[((TypeList[element_types.values].size - 1) - idx)])])
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.param_list.concat(element_types.values, other_element_types.values)])
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.