1

In cl-case clauses (though naturally not the initial keyform), keys are apparently quoted:

(let ((mark ?.))
  (case mark
    (mark 'mark)
    (?. ?.))) ; 46 (?.)

(let ((mark ?.))
  (case 'mark
    (mark 'mark)
    (?. ?.))) ; mark

My reading of neither the cl-lib manual nor CLHS entry for case describe this behavior. Is this the intended behavior of cl-case? If so, is this behavior documented? Is there a succinct way to match the keyform against a variable in a cl-case (as opposed to using a cond)?

6
  • If you don't want to use cond, why not use pcase? – Basil Jan 7 at 15:42
  • @Drew: this question is about the semantics of an elisp construct. Wouldn't that be within the scope of that tag? – outis Jan 8 at 0:23
  • The more specific tag conditionals covers that. Just like the specific tag elisp-macros is appropriate for a question about Elisp macros. – Drew Jan 8 at 4:22
  • @Drew: that's clearer, thanks. – outis Jan 8 at 4:26
2

That behavior is actually described there, albeit in terse form:

  • "Macro: cl-case keyform clause...": cl-case is a macro, macros do not evaluate their arguments.
  • "This macro evaluates KEYFORM [...]": One of the arguments is evaluated, the rest aren't.
  • Therefore the key forms aren't evaluated.

If you find yourself in that situation, just use cond and the appropriate equality predicate. To shorten matching for several keys, use memq, memql, member or cl-find with the :test keyword. In fact, I almost always use cond and only rarely resort to pattern matching constructs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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