emacs lisp: testing equality of symbol variables

When setting your emacs preferences, you can check whether your machine is {Windows, Mac, Linux}, by the variable system-type, like this:

(defun open-in-desktop ()
  "Open the current file in desktop.
Works in Microsoft Windows, Mac OS X, Linux."
  (interactive)
  (cond
   ((string-equal system-type "windows-nt")
    (w32-shell-execute "explore" (replace-regexp-in-string "/" "\\" default-directory t t)))
   ((string-equal system-type "darwin") (shell-command "open ."))
   ((string-equal system-type "gnu/linux") (shell-command "xdg-open ."))
   ) )

But i have a question. According to the “describe-variable” and elisp manual ((info "(elisp) System Environment")), the var “system-type”'s value is a symbol. However, why does the following work? (string-equal system-type "windows-nt")

It appears, any symbol can be tested as if it is a string. For example:

(string-equal 'x 'x) ; ⇒ t
(string-equal 'x 'y) ; ⇒ nil

(setq myVar 'tt )
(string-equal myVar "tt") ; ⇒ t

Ted Zlatanov, a emacs dev, comes to the rescue on the why!

From the docstring:

string-equal is a built-in function in `C source code'.

(string-equal S1 S2)

Return t if two strings have identical contents.
Case is significant, but text properties are ignored.
Symbols are also allowed; their print names are used instead.

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs