Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 483
user0721090601 Avatar asked Sep 04 '19 02:09

user0721090601


People also ask

What are truthy and Falsy values?

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 .

What Are the Truthy values?

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 .

What values are Falsy?

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.

How do I find my truthy value?

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.


2 Answers

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.

like image 174
user0721090601 Avatar answered Oct 14 '22 08:10

user0721090601


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.

like image 40
raiph Avatar answered Oct 14 '22 08:10

raiph