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:
@value
struct Foo(ImplicitlyBoolable):
var val: Bool
fn __as_bool__(self) -> Bool:
return self.val
fn __bool__(self) -> Bool:
return self.__as_bool__()
@value
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
Methods
__bool__
__bool__(self: _Self) -> Bool
Get the boolean representation of the value.
Returns:
The boolean representation of the value.
__as_bool__
__as_bool__(self: _Self) -> Bool
Get the boolean representation of the value.
Returns:
The boolean representation of the value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!