IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

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.