Skip to main content
Log in

Mojo trait

Truncable

The Truncable trait describes a type that defines a truncation operation.

Types that conform to Truncable will work with the builtin trunc function. The truncation operation always returns the same type as the input.

For example:

from math import Truncable, trunc

@value
struct Complex(Truncable):
var re: Float64
var im: Float64

fn __trunc__(self) -> Self:
return Self(trunc(self.re), trunc(self.im))
from math import Truncable, trunc

@value
struct Complex(Truncable):
var re: Float64
var im: Float64

fn __trunc__(self) -> Self:
return Self(trunc(self.re), trunc(self.im))

Implemented traits

AnyType, UnknownDestructibility

Methods

__trunc__

__trunc__(self: _Self) -> _Self

Return the truncated Int value, which is itself.

Returns:

The Int value itself.