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 "".

Drew
  • 77,472
  • 10
  • 114
  • 243
  • (let ((issue "")) (string= issue "")) returns t for me. Are you sure issue is an empty string? – choroba Feb 02 '23 at 15:26
  • I'm sure it is, please see my edit. – Reverent Lapwing Feb 02 '23 at 15:42
  • If the match fails, then issue is set to nil, not to "". – NickD Feb 02 '23 at 15:59
  • concat turns nil into "", try printing it without concat, too. – choroba Feb 02 '23 at 16:06
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Feb 02 '23 at 16:37
  • @Drew I disagree, specifics of how @string=@ works with @nil@ and @(length nil)@ equating to 0 fits the tag, since this is something that different implementations of Lisp with handle in different ways. I leave the final decision to you, but in my opinion the use of the tag was justified. – Reverent Lapwing Feb 02 '23 at 21:04
  • You're asking how to do something using Elisp, not something about the basic nature of Elisp vs other Lisps etc. Nearly every Emacs question is about how to do something using Elisp. There are specific tags for specific Elisp things (e.g. elisp-macros, let-binding, lexical-binding,...) - use those when appropriate. Be specific. – Drew Feb 03 '23 at 02:25

1 Answers1

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))
-> ""
choroba
  • 2,045
  • 11
  • 16