summaryrefslogtreecommitdiff
path: root/notes/250210-booleans.md
blob: 6ee8b2ba7268bf952a1a5e846d3c30ce9c1777c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Only Booleans have truthiness

_2025 February_

Like in Java, there should be no implicit conversion of values to a
Boolean.  This leads to sloppy code and subtle bugs.

I believe the code base of Guix contained an example of this at some
point: Build phases ending in a call to `system*` would return 0 or 1
which would pass as "true" regardless.

In most cases, you should know the actual type of the value you're
receiving, and do an appropriate check, be it `zero?`, `null?`, or
some other check.  If you truly want to check if something is "any
value other than false" then you can always do:

```scheme

(if (not (eq? #f value))
    (do something))

```

No, you cannot use `(not (not x))` because `not` obviously expects a
Boolean argument!  Duh.

I'm actually serious about this.  Scheme went all the way to make null
separate from false, but then refused to go all the way and decided to
allow non-Boolean values to function as Booleans anyway.

Of course, performing a type-check on every single conditional may
incur a serious performance penalty.  If so, then some kind of [strict
mode](250210-strict-mode.html) flag may determine whether non-Booleans
can be coerced into Booleans.