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
def write_to(self, mut writer: Some[Writer]):
writer.write("(", self.x, ", ", self.y, ")")
def 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.7Note: The default reflection-based implementations iterate over all fields
at compile time. For mutually recursive types (e.g., struct A has a field
of type List[B] and struct B has a field of type A), this creates an
infinite monomorphization cycle that causes the compiler to hang. To fix
this, provide explicit write_to() and write_repr_to() implementations
for at least one type in the cycle.
Implemented traits
AnyType,
ImplicitlyDestructible
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
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!