Rules / Types, classes & roles / basic

Bool Reference

Logical Boolean

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method ACCEPTS #

method ACCEPTS(Bool:D: --> Bool:D) Used for smartmatch comparison. When the right side is True returns always True, when the right side of the match is False returns always False. In particular, ACCEPTS returns always the instance on which it is invoked, that is the right side of a smartmatch. As an example:

routine succ #

method succ(--> Bool:D)

Returns Bool:D.

Returns True. succ is short for "successor"; it returns the next enum value. Bool is a special enum with only two values, False and True. When sorted, False comes first, so True is its successor. And since True is the "highest" Bool enum value, its own successor is also True.

routine pred #

method pred(--> Bool:D)

Returns Bool:D.

Returns False. pred is short for "predecessor"; it returns the previous enum value. Bool is a special enum with only two values, False and True. When sorted, False comes first, so False is the predecessor to True. And since False is the "lowest" Bool enum value, its own predecessor is also False.

routine enums #

method enums(--> Hash:D)

Returns Hash:D.

Returns a Hash of enum-pairs. Works on both the Bool type and any key.

routine pick #

multi method pick(Bool:U: --> Bool:D)
multi method pick(Bool:U: $count --> Seq:D)

Returns Bool:D or Seq:D.

Returns a random pick of True and/or False. If it's called without an argument then it returns just one pick: If it's called with a $count of one then it returns a Seq with just one pick: If $count is * or greater than or equal to two then it returns a Seq with two elements -- either True then False, or False then True:

routine roll #

multi method roll(Bool:U --> Bool:D)
multi method roll(Bool:U $count --> Seq:D)

Returns Bool:D or Seq:D.

Returns True or False if called without any argument. Otherwise returns $count elements chosen at random. Note that each random choice from the enum is made independently, like a separate coin toss where each side of the coin represents one of the two values of the enum. If * is passed as $count an infinite Seq of Bools is returned.

routine Int #

multi method Int(Bool:D --> Int:D)

Returns Int:D.

Returns the value part of the enum pair.

routine Numeric #

multi method Numeric(Bool:D --> Int:D)

Returns Int:D.

Returns the value part of the enum pair.

prefix ? #

multi prefix:<?>(Mu --> Bool:D)

Returns Bool:D.

Coerces its argument to Bool.

prefix so #

multi prefix:<so>(Mu --> Bool:D)

Returns Bool:D.

Coerces its argument to Bool, has looser precedence than prefix:<?>.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

2 all-differ · 1 doc-drift · 2 no-output · 5 ok · 1 rakudo-differs

enum Bool <False True>

Not executed: the documentation states no expected output for this example.

my $b = Bool.new( True );
# when True on the right side returns
# always True
True  ~~ $b;     # True
False ~~ $b;     # True

$b = Bool.new( False );
# when False on the right side
# returns always False
False ~~ $b;     # False
True ~~ $b;      # False

Not executed: the documentation states no expected output for this example.

say True.succ;                                    # OUTPUT: «True␤»
say False.succ;                                   # OUTPUT: «True␤»
Output
True
True

Documentation, Rakudo and Raku++ all agree.

say True.pred;                                    # OUTPUT: «False␤»
say False.pred;                                   # OUTPUT: «False␤»
Output
False
False

Documentation, Rakudo and Raku++ all agree.

say Bool.enums;                                   # OUTPUT: «{False => 0, True => 1}␤»
say False.enums;                                  # OUTPUT: «{False => 0, True => 1}␤»
Documentation
{False => 0, True => 1}
{False => 0, True => 1}
Rakudo
Map.new((False => 0, True => 1))
Map.new((False => 0, True => 1))
Raku++

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say Bool.pick;                                    # OUTPUT: «True␤»
Documentation
True
Rakudo
False
Raku++
True

Raku++ matches the documentation; Rakudo does not. This is the one class where neither engine can be assumed right: it may be a stale doc that Raku++ was built from, or it may be a Rakudo bug that the documentation predates. Each case is examined individually.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say Bool.pick(1);                                 # OUTPUT: «(False)␤»
Output
(False)

Documentation, Rakudo and Raku++ all agree.

say Bool.pick(*);                                 # OUTPUT: «(False True)␤»
Documentation
(False True)
Rakudo
(True False)
Raku++
(True False)

Both engines agree; the documentation states something else. Trust the engines.

say Bool.roll;                                    # OUTPUT: «True␤»
say Bool.roll(3);                                 # OUTPUT: «(True False False)␤»
say Bool.roll(*);                                 # OUTPUT: «(...)␤»
Documentation
True
(True False False)
(...)
Rakudo
True
(False True True)
(...)
Raku++
False
(True False False)
()

All three differ. Needs a human.

Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.

say False.Int;                                # OUTPUT: «0␤»
say True.Int;                                 # OUTPUT: «1␤»
Output
0
1

Documentation, Rakudo and Raku++ all agree.

say False.Numeric;                                # OUTPUT: «0␤»
say True.Numeric;                                 # OUTPUT: «1␤»
Output
0
1

Documentation, Rakudo and Raku++ all agree.