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[W: Writer](self, inout writer: W):
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, ")")
struct Point(Writable):
var x: Float64
var y: Float64
fn write_to[W: Writer](self, inout writer: W):
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
Methods
write_to
write_to[W: Writer](self: _Self, inout writer: W)
Formats the string representation of this type to the provided Writer.
Parameters:
- W (
Writer
): A type conforming to the Writable trait.
Args:
- writer (
W
): The type conforming toWritable
.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!