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:
@value
struct Foo(ImplicitlyIntable):
var i: Int
fn __as_int__(self) -> Int:
return self.i
@value
struct Foo(ImplicitlyIntable):
var i: Int
fn __as_int__(self) -> Int:
return self.i
Now you can use Foo
anywhere that an Int
is expected, e.g. equality
checks:
foo = Foo(42)
assert_equal(foo, 42)
foo = Foo(42)
assert_equal(foo, 42)
Implemented traits
AnyType
,
CollectionElement
,
Copyable
,
Intable
,
Movable
,
UnknownDestructibility
Methods
__copyinit__
__copyinit__(out self: _Self, existing: _Self, /)
Create a new instance of the value by copying an existing one.
Args:
- existing (
_Self
): The value to copy.
__moveinit__
__moveinit__(out self: _Self, owned existing: _Self, /)
Create a new instance of the value by moving the value of another.
Args:
- existing (
_Self
): The value to move.
__as_int__
__as_int__(self: _Self) -> Int
Implicitly convert to an integral representation of the value, wherever an Int
is expected.
Returns:
The integral representation of the value.
__int__
__int__(self: _Self) -> Int
Get the integral representation of the value.
Returns:
The integral representation of the value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!