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__()

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)
foo = Foo(42)
assert_equal(Int(42), foo)

Implemented traits

AnyType, Intable, 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

__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?