rakutruthiness

What are the truthy and falsy values in Raku?


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.


Solution

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

    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.