What are the truthy and falsy values in Raku?
Asked Answered
M

2

12

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.

Melba answered 4/9, 2019 at 2:33 Comment(0)
M
13

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)
  • 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.

Melba answered 4/9, 2019 at 2:42 Comment(2)
Of course any object can have it's own .Bool method and can handle it's own truthiness. And if need be you can also use composition to handle the classic DBI case : 0 but True;Treatment
@Scimon: indeed. I actually wrote this question/answer only because I was doing a blog post on a similar topic and wanted to link to a good overview like what other languages have and realized the SE question for P6 was really out of date. I talk about both of what you mention in the blog haMelba
A
8

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.

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.

Amylase answered 4/9, 2019 at 18:20 Comment(1)
+1 I didn't think the failures and promises. I'll update mine to include those as well. I think having a very reduced quick reference and your detailed answer is a good combination for people looking up things and finding this questionMelba

© 2022 - 2024 — McMap. All rights reserved.