Skip to main content

Mojo trait

Absable

The Absable trait describes a type that defines an absolute value operation.

Types that conform to Absable will work with the builtin abs function. The absolute value operation always returns the same type as the input.

For example:

struct Celsius(Absable):
    var degrees: Float64

    def __init__(out self, degrees: Float64):
        self.degrees = degrees

    def __abs__(self) -> Self:
        return Self(abs(self.degrees))

Implemented traitsโ€‹

AnyType

Required methodsโ€‹

__abs__โ€‹

__abs__(self: _Self) -> _Self

Get the absolute value of this instance.

Returns:

_Self: The absolute value of the instance.

Was this page helpful?