This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-02-02
Channels
- # announcements (4)
- # babashka (1)
- # beginners (4)
- # cider (4)
- # clerk (3)
- # clojure (25)
- # clojure-brasil (5)
- # clojure-europe (6)
- # clojure-nl (1)
- # clojure-norway (48)
- # clojure-uk (4)
- # clojuredesign-podcast (3)
- # clojurescript (1)
- # conjure (1)
- # emacs (23)
- # hyperfiddle (3)
- # jobs (6)
- # lsp (12)
- # off-topic (5)
- # polylith (13)
- # reagent (8)
- # reitit (18)
- # sci (20)
- # shadow-cljs (8)
- # specter (3)
- # squint (3)
TIL:
user> (type :kw)
clojure.lang.Keyword
user> (case (type :kw) clojure.lang.Keyword 3)
Execution error (IllegalArgumentException) at user/eval15257 (REPL:716).
No matching clause: class clojure.lang.Keyword
user> (doc case) ------------------------- clojure.core/case ([e & clauses]) Macro Takes an expression, and a set of clauses. Each clause can take the form of either: test-constant result-expr (test-constant1 ... test-constantN) result-expr The test-constants are not evaluated. They must be compile-time literals, and need not be quoted. If the expression is equal to a test-constant, the corresponding result-expr is returned. A single default expression can follow the clauses, and its value will be returned if no clause matches. If no default expression is provided and no clause matches, an IllegalArgumentException is thrown. Unlike cond and condp, case does a constant-time dispatch, the clauses are not considered sequentially. All manner of constant expressions are acceptable in case, including numbers, strings, symbols, keywords, and (Clojure) composites thereof. Note that since lists are used to group multiple constants that map to the same expression, a vector can be used to match a list if needed. The test-constants need not be all of the same type. nil user>
paradoxically, I feel like it becomes a little bit clearer what's happening when using what, at first, seems to be an even more baffling example:
(case clojure.lang.Keyword
clojure.lang.Keyword 3)
No matching clause: class clojure.lang.Keyword
I think this has been what's getting me:
They must be compile-time literals
lots of weird stuff like:
(case '(1 2) (1 2) 3)
Execution error (IllegalArgumentException) at user/eval15343 (REPL:818).
No matching clause: (1 2)
user>
oh wait lol that does work if you quote both, hmm
user> (case '(1 2) '(1 2) 3)
3
yeah... since the argument to case is evaluated, clojure.lang.Keyword is a class, but as a case, it's not evaluated (the compile-time literals part), so it's a symbol
(let [v '(1)]
(case v
'(1) \a
'(2) \b))
Syntax error macroexpanding case at (*cider-repl projects/lrsql:localhost:40521(clj)*:835:9).
Duplicate case test constant: quote
right, because '(1)
reads as (quote (1)
, the actual case will match the symbol quote or a list of 1
ok that's weird
it can probably qualify for "unintuitive", and I think it's one of those things that's easy to trip over, but good to have when you need it
A similar issue occurs with Java enums, so there are valid reasons to write (condp = val ...)
instead of (case val ...)
Beware that ClojureScript's case
may evaluate constant literals! In practice, if the code might wind up in cljc, avoid symbols in case
.
Hello. Could anyone help me understand what’s going on here? Is accessing static class fields with function call syntax an undocumented feature?
user=> java.text.BreakIterator/DONE
-1
user=> (java.text.BreakIterator/DONE)
-1
user=> (ifn? java.text.BreakIterator/DONE)
false
user=> (let [x java.text.BreakIterator] (x))
Execution error (ClassCastException) at user/eval144 (REPL:1).
user=> ((java.text.BreakIterator/DONE))
Execution error (ClassCastException) at user/eval146 (REPL:1).
java.text.BreakIterator.DONE
is a static final int
Thank you!
Nice, thanks Ben
You're welcome