Mojo trait
CoordLike
Trait for unified layout handling of compile-time and runtime indices.
Implemented traits
AnyType,
Copyable,
Defaultable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable,
RegisterPassable,
TrivialRegisterPassable,
Writable
comptime members
DTYPE
comptime DTYPE = DType.invalid
The data type for runtime values, or invalid for compile-time values.
is_static_value
comptime is_static_value = False
True if the value is known at compile time.
is_tuple
comptime is_tuple = False
True if this is a tuple type (Coord), False for scalar values.
is_value
comptime is_value = not _Self.is_tuple.__bool__()
True if this is a scalar value, False for tuple types.
static_value
comptime static_value
The compile-time value if statically known, -1 otherwise.
VariadicType
comptime VariadicType
The variadic element types for tuple coordinates.
Required methods
__init__
__init__(out self: _Self, *, copy: _Self)
Create a new instance of the value by copying an existing one.
Args:
- copy (
_Self): The value to copy.
Returns:
_Self
__init__(out self: _Self)
Create a default instance of the value.
Returns:
_Self
__init__(out self: _Self, *, deinit take: _Self)
Create a new instance of the value by moving the value of another.
Args:
- take (
_Self): The value to move.
Returns:
_Self
__len__
static __len__() -> Int
Get the number of elements in this type.
Returns:
Int: The number of elements (1 for single values, >1 for tuples).
value
value(self: _Self) -> Int
Get the integer value of this coordinate.
Only valid for value types (not tuples).
Returns:
Int: The integer value.
tuple
tuple(var self: _Self) -> Coord[_Self.VariadicType]
Get this coordinate as a Coord tuple.
Only valid for tuple types.
Returns:
Coord: The coordinate as a Coord tuple.
product
product(self: _Self) -> Int
Calculate the product of all elements.
Returns:
Int: The product of all elements.
sum
Provided methods
copy
copy(self: _Self) -> _Self
Explicitly construct a copy of self, a convenience method for Self(copy=self) when the type is inconvenient to write out.
Returns:
_Self: A copy of this value.
write_to
write_to(self: _Self, mut writer: T)
Write this value's text representation to a writer.
This method is called by print(), String(), and format strings to
convert the value to text. Override this method to define how your type
appears when printed or converted to a string.
The default implementation uses reflection to format all fields as
TypeName(field1=value1, field2=value2, ...), calling write_to()
on each field. All fields must conform to Writable.
Example
def write_to(self, mut writer: Some[Writer]):
writer.write("(", self.x, ", ", self.y, ")")Args:
- writer (
T): The destination for formatted output.
write_repr_to
write_repr_to(self: _Self, mut writer: T)
Write this value's debug representation to a writer.
This method is called by repr(value) or the "{!r}" format specifier
and should produce unambiguous, developer-facing output that shows the
internal state of the value.
The default implementation uses reflection to format all fields as
TypeName(field1=value1, field2=value2, ...), calling write_repr_to()
on each field. All fields must conform to Writable.
Example
def write_repr_to(self, mut writer: Some[Writer]):
writer.write("Point: x=", self.x, ", y=", self.y)Notes:
Mojo's repr always prints single quotes (') at the start and end
of the repr. Any single quote inside a string should be escaped
(\').
Args:
- writer (
T): The destination for formatted output.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!