While it is always possible to use mixins or method overrides to modify the Bool coercions, by default what values are considered to be truthy and what values are considered to be falsy?
Note: this question was asked previously, but unfortunately it is so old its content is completely out of date and useless is modern Raku.
Truthy values are values that evaluate to True in a boolean context. Falsy values are values that evaluate to False in a boolean context. Falsy values include empty sequences (lists, tuples, strings, dictionaries, sets), zero in every numeric type, None , and False .
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false , 0 , -0 , 0n , "" , null , undefined , and NaN .
A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined , null , NaN , 0 , "" (empty string), and false of course.
To check if a value is truthy, pass the value to an if statement, e.g. if (myValue) . If the value is truthy, it gets coerced to true and runs the if block. Copied! In our if statement, we check if the value in the myVar variable is truthy.
There are no truthy values, as each type decides for itself via a .Bool
method that is called in boolean contexts. For built-in types, the following return False
when their .Bool
method is called.
0
(except Rat
of x/0
where x≠0
)Failure
Promise
prior to being kept/broken.StrDistance
whose before/after is the same.Junction
, when you expect it to.Otherwise, any defined value by default returns True
when its .Bool
method is called. This includes the Str
'0'
, NaN
, and zero-length range (0^..^0
) that in other languages might not be truthy.
This answer is intended as a quick reference. See this answer for a more detailed discussion.
TL;DR This answer is an exhaustive summary based on the relevant doc.1
The base case2 is True
for a defined object (an instance) and False
for an undefined one (a type object).
Numerically 0
values or 0/0
are False
. (But a Rational
with a non-zero numerator eg 1/0
is True
and (0/0).Num
(which evaluates to NaN
) is True
.)
An empty collection (List
, Hash
, Set
, Buf
, etc) is False
.
An empty string (eg literal ""
) is False
. (NB. "0"
, "0.0"
etc. are True
.)
A defined Failure
is False
.
A defined Promise
is False
until its status becomes Kept
/Broken
.
A defined StrDistance
is False
if the string transformation it represents had no effect on the string being transformed.
A defined Junction
is True
or False
depending on the junction's type and the True
/False
values of its elements.
1 I wrote the first bullet item based on just knowing it to be true because it's fundamental to P6 and also confirming it by checking the compiler's code.2 The other bullet points summarize the content at the time of writing this answer of the .Bool
doc page at which point it listed 20 types. If the latter page was incomplete then this answer is incomplete.
2 The base case can be seen by looking at the Rakudo implementation code, in particular the core's Mu.pm6
. See my answer to a similarish SO for relevant code links.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With