Skip to main content

Mojo trait

ImplicitlyBoolable

The ImplicitlyBoolable trait describes a type that can be implicitly converted to a Bool.

Types conforming to this trait can be passed to a function that expects a Bool without explicitly converting to it. Accordingly, most types should conform to Boolable instead, since implicit conversions to Bool can have unintuitive consequences.

This trait requires the type to implement the __as_bool__() method. For example:

struct Foo(ImplicitlyBoolable):
    var val: Bool

    fn __as_bool__(self) -> Bool:
        return self.val

    fn __bool__(self) -> Bool:
        return self.__as_bool__()

Implemented traits

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

__bool__

__bool__(self: _Self) -> Bool

Get the boolean representation of the value.

Returns:

Bool: The boolean representation of the value.

__as_bool__

__as_bool__(self: _Self) -> Bool

Get the boolean representation of the value.

Returns:

Bool: The boolean representation of the value.

Was this page helpful?