Skip to main content

Mojo trait

Writable

A trait for types that can format themselves as text.

The Writable trait provides a simple, straightforward interface for types that need to convert themselves to text. Types implementing Writable write directly to a Writer, making formatting efficient and allocation-free.

Both write_to() and write_repr_to() have default implementations that use reflection to automatically format all fields. This means simple structs can conform to Writable without any method implementations:

@fieldwise_init
struct Point(Writable):
    var x: Int
    var y: Int

var p = Point(1, 3)
print(p)       # Point(x=1, y=3)
print(repr(p)) # Point(x=Int(1), y=Int(3))

Override either for different normal and debug output:

@fieldwise_init
struct Point(Writable):
    var x: Float64
    var y: Float64

    fn write_to(self, mut writer: Some[Writer]):
        writer.write("(", self.x, ", ", self.y, ")")

    fn write_repr_to(self, mut writer: Some[Writer]):
        writer.write("Point: x=", self.x, ", y=", self.y)

var p = Point(1.5, 2.7)
print(p)       # (1.5, 2.7)
print(repr(p)) # Point: x=1.5, y=2.7

Implemented traits

AnyType, ImplicitlyDestructible

comptime members

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

Provided methods

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.

Was this page helpful?