IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

IntableRaising

The IntableRaising trait describes a type can be converted to an Int, but the conversion might raise an error.

Any type that conforms to Intable or IntableRaising can construct an Int.

This trait requires the type to implement the __int__() method, which can raise an error. For example:

@fieldwise_init
struct Foo(IntableRaising):
var i: Int

def __int__(self) raises -> Int:
return self.i

Now you can construct an Int:

@fieldwise_init
struct Foo(IntableRaising):
var i: Int

def __int__(self) raises -> Int:
return self.i

foo = Foo(42)
assert_equal(Int(foo), 42)

Implemented traits

AnyType

Required methods

__int__

__int__(self: _Self) -> Int

Get the integral representation of the value.

Returns:

Int: The integral representation of the type.

Raises:

If the type does not have an integral representation.