Skip to main content
Log in

Mojo struct

Bool

@register_passable(trivial) struct Bool

The primitive Bool scalar value used in Mojo.

Fields

  • value (i1): The underlying storage of the boolean value.

Implemented traits

AnyType, Boolable, CollectionElement, CollectionElementNew, Comparable, ComparableCollectionElement, Copyable, Defaultable, EqualityComparable, ExplicitlyCopyable, Floatable, GreaterThanComparable, GreaterThanOrEqualComparable, ImplicitlyBoolable, ImplicitlyIntable, Indexer, Intable, LessThanComparable, LessThanOrEqualComparable, Movable, Representable, Stringable, UnknownDestructibility, Writable

Methods

__init__

__init__() -> Self

Construct a default, False Bool.

@implicit __init__[T: ImplicitlyBoolable, //](value: T) -> Self

Convert an ImplicitlyBoolable value to a Bool.

Parameters:

  • T (ImplicitlyBoolable): The ImplicitlyBoolable type.

Args:

  • value (T): The boolable value.

__init__[T: Boolable, //](value: T) -> Self

Set the bool representation of the object.

Parameters:

  • T (Boolable): The type of the object.

Args:

  • value (T): The object to get the bool representation of.

__init__(value: None) -> Self

Set the bool representation of the None type to False.

Args:

  • value (None): The object to get the bool representation of.

@implicit __init__(value: SIMD[bool, 1]) -> Self

Convert a scalar SIMD value to a Bool.

Args:

  • value (SIMD[bool, 1]): The scalar value.

__bool__

__bool__(self) -> Self

Convert to Bool.

Returns:

This value.

__neg__

__neg__(self) -> Int

Defines the unary - operation.

Returns:

0 for -False and -1 for -True.

__invert__

__invert__(self) -> Self

Inverts the Bool value.

Returns:

True if the object is false and False otherwise.

__lt__

__lt__(self, rhs: Self) -> Self

Compare this Bool to RHS using less-than comparison.

Args:

  • rhs (Self): The rhs of the operation.

Returns:

True if self is False and rhs is True.

__le__

__le__(self, rhs: Self) -> Self

Compare this Bool to RHS using less-than-or-equal comparison.

Args:

  • rhs (Self): The rhs of the operation.

Returns:

True if self is False and rhs is True or False.

__eq__

__eq__(self, rhs: Self) -> Self

Compare this Bool to RHS.

Performs an equality comparison between the Bool value and the argument. This method gets invoked when a user uses the == infix operator.

Args:

  • rhs (Self): The rhs value of the equality statement.

Returns:

True if the two values match and False otherwise.

__ne__

__ne__(self, rhs: Self) -> Self

Compare this Bool to RHS.

Performs a non-equality comparison between the Bool value and the argument. This method gets invoked when a user uses the != infix operator.

Args:

  • rhs (Self): The rhs value of the non-equality statement.

Returns:

False if the two values do match and True otherwise.

__gt__

__gt__(self, rhs: Self) -> Self

Compare this Bool to RHS using greater-than comparison.

Args:

  • rhs (Self): The rhs of the operation.

Returns:

True if self is True and rhs is False.

__ge__

__ge__(self, rhs: Self) -> Self

Compare this Bool to RHS using greater-than-or-equal comparison.

Args:

  • rhs (Self): The rhs of the operation.

Returns:

True if self is True and rhs is True or False.

__and__

__and__(self, rhs: Self) -> Self

Returns self & rhs.

Bitwise and's the Bool value with the argument. This method gets invoked when a user uses the and infix operator.

Args:

  • rhs (Self): The right hand side of the and statement.

Returns:

self & rhs.

__or__

__or__(self, rhs: Self) -> Self

Returns self | rhs.

Bitwise or's the Bool value with the argument. This method gets invoked when a user uses the or infix operator.

Args:

  • rhs (Self): The right hand side of the or statement.

Returns:

self | rhs.

__xor__

__xor__(self, rhs: Self) -> Self

Returns self ^ rhs.

Bitwise Xor's the Bool value with the argument. This method gets invoked when a user uses the ^ infix operator.

Args:

  • rhs (Self): The right hand side of the xor statement.

Returns:

self ^ rhs.

__rand__

__rand__(self, lhs: Self) -> Self

Returns lhs & self.

Args:

  • lhs (Self): The left hand side of the and statement.

Returns:

lhs & self.

__ror__

__ror__(self, lhs: Self) -> Self

Returns lhs | self.

Args:

  • lhs (Self): The left hand side of the or statement.

Returns:

lhs | self.

__rxor__

__rxor__(self, lhs: Self) -> Self

Returns lhs ^ self.

Args:

  • lhs (Self): The left hand side of the xor statement.

Returns:

lhs ^ self.

__iand__

__iand__(mut self, rhs: Self)

Computes self & rhs and store the result in self.

Args:

  • rhs (Self): The right hand side of the and statement.

__ixor__

__ixor__(mut self, rhs: Self)

Computes self ^ rhs and stores the result in self.

Args:

  • rhs (Self): The right hand side of the xor statement.

__ior__

__ior__(mut self, rhs: Self)

Computes self | rhs and store the result in self.

Args:

  • rhs (Self): The right hand side of the or statement.

copy

copy(self) -> Self

Explicitly construct a deep copy of the provided value.

Returns:

A copy of the value.

__as_bool__

__as_bool__(self) -> Self

Convert to Bool.

Returns:

This value.

__str__

__str__(self) -> String

Get the bool as a string.

Returns "True" or "False".

Returns:

A string representation.

write_to

write_to[W: Writer](self, mut writer: W)

Formats this boolean to the provided Writer.

Parameters:

  • W (Writer): A type conforming to the Writable trait.

Args:

  • writer (W): The object to write to.

__repr__

__repr__(self) -> String

Get the bool as a string.

Returns "True" or "False".

Returns:

A string representation.

__int__

__int__(self) -> Int

Convert this Bool to an integer.

Returns:

1 if the Bool is True, 0 otherwise.

__as_int__

__as_int__(self) -> Int

Implicitly convert to an integral representation of the value, wherever an Int is expected.

Returns:

The integral representation of the value.

__float__

__float__(self) -> SIMD[float64, 1]

Convert this Bool to a float.

Returns:

1.0 if True else 0.0 otherwise.

__index__

__index__(self) -> index

Convert to index.

Returns:

1 if the Bool is True, 0 otherwise.

__hash__

__hash__[H: _Hasher](self, mut hasher: H)

Updates hasher with the underlying bytes.

Parameters:

  • H (_Hasher): The hasher type.

Args:

  • hasher (H): The hasher instance.