What are the truthy and falsy values in Raku?
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
(exceptRat
ofx/0
wherex≠0
)- Empty list/collection types (List, Array, Hash, Map, Set, Bag, etc)
- Empty string
Failure
Promise
prior to being kept/broken.StrDistance
whose before/after is the same.Junction
, when you expect it to.- Type objects
- Nil (technically type object)
- Any undefined value (technically type objects)
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) andFalse
for an undefined one (a type object).Numerically
0
values or0/0
areFalse
. (But aRational
with a non-zero numerator eg1/0
isTrue
and(0/0).Num
(which evaluates toNaN
) isTrue
.)An empty collection (
List
,Hash
,Set
,Buf
, etc) isFalse
.An empty string (eg literal
""
) isFalse
. (NB."0"
,"0.0"
etc. areTrue
.)A defined
Failure
isFalse
.A defined
Promise
isFalse
until its status becomesKept
/Broken
.A defined
StrDistance
isFalse
if the string transformation it represents had no effect on the string being transformed.A defined
Junction
isTrue
orFalse
depending on the junction's type and theTrue
/False
values of its elements.
Footnotes
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.