Bool

Boolean type and operations.

Import from the base library to use this module.

motoko name=import
import Bool "mo:base/Bool";

While boolean operators _ and _ and _ or _ are short-circuiting, avoiding computation of the right argument when possible, the functions logand(_, _) and logor(_, _) are strict and will always evaluate both of their arguments.

Example:

motoko include=import
let t = true;
let f = false;

// Short-circuiting AND
assert not (t and f);

// Short-circuiting OR
assert t or f;

type Bool = Prim.Types.Bool

Booleans with constants true and false.

public func logicalAnd(a : Bool, b : Bool) : Bool

Returns a and b.

Example:

motoko include=import
assert not Bool.logicalAnd(true, false);
assert Bool.logicalAnd(true, true);

public func logicalOr(a : Bool, b : Bool) : Bool

Returns a or b.

Example:

motoko include=import
assert Bool.logicalOr(true, false);
assert Bool.logicalOr(false, true);

public func logicalXor(a : Bool, b : Bool) : Bool

Returns exclusive or of a and b, a != b.

Example:

motoko include=import
assert Bool.logicalXor(true, false);
assert not Bool.logicalXor(true, true);
assert not Bool.logicalXor(false, false);

public func logicalNot(bool : Bool) : Bool

Returns not bool.

Example:

motoko include=import
assert Bool.logicalNot(false);
assert not Bool.logicalNot(true);

public func equal(a : Bool, b : Bool) : Bool

Returns a == b.

Example:

motoko include=import
assert Bool.equal(true, true);
assert not Bool.equal(true, false);

public func compare(a : Bool, b : Bool) : Order.Order

Returns the ordering of a compared to b. Returns #less if a is false and b is true, #equal if a equals b, and #greater if a is true and b is false.

Example:

motoko include=import
assert Bool.compare(true, false) == #greater;
assert Bool.compare(true, true) == #equal;
assert Bool.compare(false, true) == #less;

public func toText(bool : Bool) : Text

Returns a text value which is either "true" or "false" depending on the input value.

Example:

motoko include=import
assert Bool.toText(true) == "true";
assert Bool.toText(false) == "false";

public func allValues() : Iter.Iter<Bool>

Returns an iterator over all possible boolean values (true and false).

Example:

motoko include=import
let iter = Bool.allValues();
assert iter.next() == ?true;
assert iter.next() == ?false;
assert iter.next() == null;