DWIM(.nl) - 0 is true

DWIM(.nl)

Do What I Mean

June 27th, 2021

0 is true

I was reading, as one does on a Sunday morning, An Introduction to Programming in Emacs Lisp and came across the statement “false” is just our old friend nil. Anything else—anything at all—is “true”.

Which made me blink, because in most other programming languages this is not the case. So, I checked, and found the statement to be correct (duh\trade):

 *** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (not nil)
t
ELISP> (not 0)
nil
ELISP> (not "")
nil

I would expect elisp to be similar to other scripted languages. For example, Javascript has a whole list of falsy values, including (testing with negation, to cast to boolean):

Welcome to Node.js v12.21.0.
Type ".help" for more information.
> !undefined
true
> !0
true
> !""
true

Or in Python:

Python 3.9.5 (default, May 11 2021, 08:20:37)
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> not None
True
>>> not 0
True
>>> not ""
True

Or maybe elisp would be closer to C, because that's what its base functions are written in. But even that is a bit different:

  #include <stdio.h>

  int main() {
    printf("NULL = %d\n", !NULL);
    printf("0 = %d\n", !0);
    printf("\"\" = %d\n", !"");
  }

gives

  NULL = 1
  0 = 1
  "" = 0

So, elisp is unlike any other language I know. Weird (but so are C, Python and JS). And good to be aware of.

tags: emacs, dev

 I bit the bullet 
 Still learning 
Loading script...