Skip to main content

Mojo trait

ImplicitlyIntable

The ImplicitlyIntable trait describes a type that can be converted to an Int implicitly.

This trait requires the type to implement the __as_int__() method. For example:

struct Foo(ImplicitlyIntable):
    var i: Int

    fn __int__(self) -> Int:
        return self.i

    fn __as_int__(self) -> Int:
        return self.__int__()

Now you can use Foo anywhere that an Int is expected, e.g. equality checks:

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

Implemented traits

AnyType, Intable, UnknownDestructibility

Methods

__as_int__

__as_int__(self: _Self) -> Int

Implicitly convert to an integral representation of the value, wherever an Int is expected.

Returns:

Int: The integral representation of the value.

__int__

__int__(self: _Self) -> Int

Get the integral representation of the value.

Returns:

Int: The integral representation of the value.

Was this page helpful?