IntervalElement | Modular

Mojo trait

The trait denotes a trait composition of the Copyable, Writable, Intable, and Comparable traits. Which is also subtractable.

Implemented traits

AnyType, Comparable, Copyable, Equatable, ImplicitlyDestructible, Intable, Movable, Writable

comptime members

__copy_ctor_is_trivial

comptime __copy_ctor_is_trivial

A flag (often compiler generated) to indicate whether the implementation of the copy constructor is trivial.

A copy constructor is considered to be trivial if:

  • The struct has a compiler-generated trivial copy constructor because all its fields have trivial copy constructors.

In practice, it means the value can be copied by copying the bits from one location to another without side effects.

__del__is_trivial

comptime __del__is_trivial

A flag (often compiler generated) to indicate whether the implementation of __del__ is trivial.

The implementation of __del__ is considered to be trivial if:

  • The struct has a compiler-generated trivial destructor and all its fields have a trivial __del__ method.

In practice, it means that the __del__ can be considered as no-op.

__move_ctor_is_trivial

comptime __move_ctor_is_trivial

A flag (often compiler generated) to indicate whether the implementation of move constructor is trivial.

The implementation of a move constructor is considered to be trivial if:

  • The struct has a compiler-generated trivial move constructor because all its fields have trivial move constructors.

In practice, it means the value can be moved by moving the bits from one location to another without side effects.

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, *, 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

__lt__

__lt__(self: _Self, rhs: _Self) -> Bool

Define whether self is less than rhs.

Args:

  • rhs (_Self): The value to compare with.

Returns:

Bool: True if self is less than rhs.

__sub__

__sub__(self: _Self, rhs: _Self) -> _Self

Subtracts rhs from self, must be implemented in concrete types.

Args:

  • rhs (_Self): The value to subtract from self.

Returns:

_Self: The result of subtracting rhs from self.

__int__

__int__(self: _Self) -> Int

Get the integral representation of the value.

Returns:

Int: The integral representation of the value.

Provided methods

__le__

__le__(self: _Self, rhs: _Self) -> Bool

Define whether self is less than or equal to rhs.

Args:

  • rhs (_Self): The value to compare with.

Returns:

Bool: True if self is less than or equal to rhs.

__eq__

__eq__(self: _Self, other: _Self) -> Bool

Define whether two instances of the object are equal to each other.

The default implementation uses reflection to compare all fields for equality. All fields must conform to Equatable.

Args:

  • other (_Self): Another instance of the same type.

Returns:

Bool: True if the instances are equal according to the type's definition of equality, False otherwise.

__ne__

__ne__(self: _Self, other: _Self) -> Bool

Define whether two instances of the object are not equal to each other.

Args:

  • other (_Self): Another instance of the same type.

Returns:

Bool: True if the instances are not equal according to the type's definition of equality, False otherwise.

__gt__

__gt__(self: _Self, rhs: _Self) -> Bool

Define whether self is greater than rhs.

Args:

  • rhs (_Self): The value to compare with.

Returns:

Bool: True if self is greater than rhs.

__ge__

__ge__(self: _Self, rhs: _Self) -> Bool

Define whether self is greater than or equal to rhs.

Args:

  • rhs (_Self): The value to compare with.

Returns:

Bool: True if self is greater than or equal to rhs.

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

fn 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

fn 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.