Skip to main content

trait

StringableRaising

The StringableRaising trait describes a type that can be converted to a String.

Any type that conforms to Stringable or StringableRaising works with the built-in print() and str() functions.

The StringableRaising trait requires the type to define the __str__() method, which can raise an error. For example:

@value
struct Foo(StringableRaising):
var s: String

fn __str__(self) raises -> String:
if self.s == "":
raise Error("Empty String")
return self.s

Now you can pass an instance of Foo to the str() function to get back a String:

fn main() raises:
var foo = Foo("test")
print(str(foo) == "test")
True

Implemented traits

AnyType

Methods

__str__

__str__(self: T) -> String

Get the string representation of the type.

Returns:

The string representation of the type.

Raises:

If there is an error when computing the string representation of the type.