Case statement/form errors?

57 views Asked by At
emacs 29.1
sbcl 2.4.0 
slime 2.29.1

Here's my function:

(defun my-case ()
  (case 'a
    (b "hello")
    (a "world")
    (otherwise "mars")))

When I compile it, C-c C-k, I see the following in the mini-buffer:

enter image description here

In the slime repl, I see:

; processing (DEFUN MY-CASE ...)

; file: /var/tmp/slimeckqotJ
; in: DEFUN MY-CASE
;     (CASE 'A (B "hello") (A "world") (OTHERWISE "mars"))
; --> COND IF PROGN 
; ==>
;   "hello"
; 
; note: deleting unreachable code

; --> COND IF IF THE PROGN 
; ==>
;   "mars"
; 
; note: deleting unreachable code
; 
; compilation unit finished
;   printed 2 notes

And, in my source file, the word "case" is underlined:

enter image description here

However, my function runs fine:

CL-USER> (my-case)
"world"

What are "notes"?

1

There are 1 answers

0
ad absurdum On BEST ANSWER

The notes are telling you that the clauses (b "hello") and (otherwise "mars") are unreachable.

The first argument to a case form is evaluated to produce a key for testing the clauses. In my-case that keyform argument always evaluates to the symbol a, so the compiler can see that there is only one possible result.

A case form is not much use when there is only one possible outcome. Here is a version that does not trigger the compiler notes and removal of unreachable code:

(defun my-case (&optional (k 'a))
   (case k
     (b "hello")
     (a "world")
     (otherwise "mars")))
CL-USER> (my-case)
"world"
CL-USER> (my-case 'b)
"hello"
CL-USER> (my-case 'needs-guitars)
"mars"