BrowserREPL-button-tip-on BrowserREPL-button-tip-off

EditButtonTip 2024-05-28

enum Bool

Logical Boolean

enum Bool <False True>
Code

An enum for Boolean true/false decisions.

Methods§

method ACCEPTS§

method ACCEPTS(Bool:D: --> Bool:D)
Code

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:

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
Code

routine succ§

method succ(--> Bool:D)
Code

Returns True.

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

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)
Code

Returns False.

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

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)
Code

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

say Bool.enums;                                   # OUTPUT: «{False => 0, True => 1}␤»
say False.enums;                                  # OUTPUT: «{False => 0, True => 1}␤»
Code

routine pick§

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

Returns a random pick of True and/or False.

If it's called without an argument then it returns just one pick:

say Bool.pick;                                    # OUTPUT: «True␤»
Code

If it's called with a $count of one then it returns a Seq with just one pick:

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

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:

say Bool.pick(*);                                 # OUTPUT: «(False True)␤»
Code

routine roll§

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

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.

say Bool.roll;                                    # OUTPUT: «True␤»
say Bool.roll(3);                                 # OUTPUT: «(True False False)␤»
say Bool.roll(*);                                 # OUTPUT: «(...)␤»
Code

routine Int§

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

Returns the value part of the enum pair.

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

routine Numeric§

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

Returns the value part of the enum pair.

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

Operators§

prefix ?§

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

Coerces its argument to Bool.

prefix so§

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

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

Class relation diagram§

Rakudo Type Relation for Bool Bool Bool Int Int Bool->Int is