Skip to main content

Mojo trait

Intable

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

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

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

struct Foo(Intable):
    var i: Int

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

Now you can construct an Int:

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

Note: If the __int__() method can raise an error, use the IntableRaising trait instead.

Implemented traits

AnyType, UnknownDestructibility

Aliases

__del__is_trivial

alias __del__is_trivial

A flag (often compiler generated) to indicate whether the implementation of __del__ is trivial.

The implementation of __del__ is considered to be trivial if:

  • The struct has a compiler-generated trivial destructor and all its fields have a trivial __del__ method.

In practice, it means that the __del__ can be considered as no-op.

Methods

__int__

__int__(self: _Self) -> Int

Get the integral representation of the value.

Returns:

Int: The integral representation of the value.

Was this page helpful?