diff options
| author | Taylan Kammer <taylan.kammer@gmail.com> | 2025-03-29 11:10:24 +0100 |
|---|---|---|
| committer | Taylan Kammer <taylan.kammer@gmail.com> | 2025-03-29 11:10:24 +0100 |
| commit | 451aa92846b5fd5c8a0739336de3aa26d741d750 (patch) | |
| tree | 21e51213bf1d39c2a8677060c51d83a656873786 /notes/booleans.md | |
| parent | 5025f9acf31cd880bbff62ff47ed03b69a0025ee (diff) | |
Relocate MD sources for HTML notes.
Diffstat (limited to 'notes/booleans.md')
| -rw-r--r-- | notes/booleans.md | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/notes/booleans.md b/notes/booleans.md new file mode 100644 index 0000000..61b9d7e --- /dev/null +++ b/notes/booleans.md @@ -0,0 +1,32 @@ +# Only Booleans have truthiness + +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 the same flag that +determines [whether returned values can be ignored](strict-mode.html) +may also determine whether non-Booleans can be coerced into Booleans. |
