Mojo 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
String() functions.
The StringableRaising trait requires the type to define the __str__()
method, which can raise an error. For example:
struct Foo(StringableRaising):
var s: String
fn __str__(self) raises -> String:
if self.s == "":
raise Error("Empty String")
return self.sNow you can pass an instance of Foo to the String() function to get back a
String:
def main():
var foo = Foo("test")
print(String(foo) == "test")TrueDeprecated: StringableRaising is deprecated, use Writable instead
Implemented traitsโ
Required methodsโ
__str__โ
__str__(self: _Self) -> String
Get the string representation of the type.
Returns:
String: The string representation of the type.
Raises:
If there is an error when computing the string representation of the type.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!