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)?

Drew
  • 75,699
  • 9
  • 109
  • 225
outis
  • 178
  • 8
  • If you don't want to use `cond`, why not use [`pcase`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Pattern_002dMatching-Conditional.html)? – Basil Jan 07 '21 at 15:42
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Jan 07 '21 at 16:41
  • @Drew: this question is about the semantics of an elisp construct. Wouldn't that be within the scope of that tag? – outis Jan 08 '21 at 00: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 08 '21 at 04:22
  • @Drew: that's clearer, thanks. – outis Jan 08 '21 at 04:26
  • (The problem with using tag `elisp` for anything that has to do with Elisp is that nearly everything in Emacs has to do with Elisp. The tag has little utility if it's used for everything.) – Drew Jan 08 '21 at 04:36

1 Answers1

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.

wasamasa
  • 21,803
  • 1
  • 65
  • 97