Skip to main content

Mojo trait

Writable

The Writable trait describes how a type is written into a Writer.

You must implement write_to which takes self and a type conforming to Writer:

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

    fn write_to(self, mut writer: Some[Writer]):
        var string = "Point"
        # Write a single `Span[Byte]`:
        writer.write_bytes(string.as_bytes())
        # Pass multiple args that can be converted to a `Span[Byte]`:
        writer.write("(", self.x, ", ", self.y, ")")

Implemented traits

AnyType, UnknownDestructibility

Aliases

__del__is_trivial

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

Methods

write_to

write_to(self: _Self, mut writer: T)

Formats the string representation of this type to the provided Writer.

Args:

  • writer (T): The type conforming to Writable.

Was this page helpful?