0

I want to understand why

(when (string= issue "")
    (error "Issue number missing"))

doesn't throw an error when issue is empty, but

(when (= (length issue) 0)
    (error "Issue number missing"))

works as expected.

Documentation of string= states that it compares content of both strings and returns t when they are identical. I'm pretty sure a string of length 0 and "" have identical contents.

The value of issue is set in the following code:

(string-match "\#\\([0-9]*\\)" text)
(setq issue (match-string 1 text))

I tried to replace when with if and added (print (concat "\"" issue "\"")) to the else clause, it printed "\"\"" as a result. I printed the POST query that uses issue as one of its inputs and it generated empty string in its place (the code constructing the query shouldn't even execute if string= returned the correct value). Not to mention that it's length is equal to 0. I'm certain that issue is equal to "".

7
  • (let ((issue "")) (string= issue "")) returns t for me. Are you sure issue is an empty string?
    – choroba
    Commented Feb 2, 2023 at 15:26
  • I'm sure it is, please see my edit. Commented Feb 2, 2023 at 15:42
  • If the match fails, then issue is set to nil, not to "".
    – NickD
    Commented Feb 2, 2023 at 15:59
  • concat turns nil into "", try printing it without concat, too.
    – choroba
    Commented Feb 2, 2023 at 16:06
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Feb 2, 2023 at 16:37

1 Answer 1

2

Empty string compares correctly to an empty string:

(let ((issue "")) (string= issue "")) 
-> t

issue is nil, not empty.

(string= nil "")
-> nil

The test is misleading because concat stringifies nil to an empty string:

(print nil)
-> nil
(print (concat nil))
-> ""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.