This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-20
Channels
- # announcements (16)
- # aws (1)
- # babashka (24)
- # beginners (54)
- # calva (25)
- # cider (4)
- # cljs-dev (2)
- # clojure (16)
- # clojure-dev (39)
- # clojure-europe (18)
- # clojure-nl (1)
- # clojure-uk (4)
- # clojurescript (66)
- # core-async (16)
- # cursive (13)
- # datomic (15)
- # deps-new (1)
- # duct (22)
- # fulcro (20)
- # jobs-discuss (1)
- # kaocha (3)
- # lsp (11)
- # malli (8)
- # off-topic (38)
- # other-languages (18)
- # polylith (18)
- # portal (15)
- # react (2)
- # reagent (9)
- # reitit (7)
- # releases (10)
- # reveal (1)
- # sci (13)
- # shadow-cljs (3)
- # specter (4)
- # sql (4)
- # xtdb (16)
Not sure if I should post this in #cljs-dev or here :)
It seems CLJS case
behaves different with respect to ^:const
than JVM Clojure.
https://github.com/clj-kondo/clj-kondo/issues/1388#issuecomment-923203438
Is this a bug in CLJS or an intentional difference?
@mfikes
It is a bug that when reported, the issue was closed because fixing it will break existing code
should a linter account for "people expect this to work in CLJS" or should it say: consider the const unused when it's only used in the case here?
it seems to be intentionally included here: https://clojure.atlassian.net/browse/CLJS-806
and just makes const inline with how case on cljs treats symbols generally, which is at odds with how case treats symbols in clojure
I am surprised that CLJS intentionally was made to behave different from Clojure here.
https://clojure.atlassian.net/browse/CLJS-2209 huh, maybe it is only the const case, but I could have sworn
@hiredman oh wow,
(def ^:const ALPHA 3)
(defn f [x]
(case x
ALPHA 1
;; :foo 2
2))
(prn (f 3))
if you uncomment the line in the case expression, then the last expression will return 2, else 1...
documented here by @mfikes https://blog.fikesfarm.com/posts/2015-06-15-clojurescript-case-constants.html
all in all, it seems easy to shoot yourself in the foot hereClojureScript 1.10.758
cljs.user=> (def ^:const x 1)
#'cljs.user/x
cljs.user=> (case 1 x 1)
1
cljs.user=> (let [x 2] (case 1 x 1))
Execution error (Error) at (<cljs repl>:1).
No matching clause: 1
cljs.user=>
is also fun@U050ECB92 can you clarify what is the second example? I lost track
cljs.user=> (str (fn [] (let [x 2] (case 1 x 1))))
"function (){\nvar x = (2);\nvar G__23 = (1);\nif(cljs.core._EQ_.call(null,new cljs.core.Symbol(null,\"x\",\"x\",(-555367584),null),G__23)){\nreturn (1);\n} else {\nthrow (new Error([\"No matching clause: \",cljs.core.str.cljs$core$IFn$_invoke$arity$1(G__23)].join('')));\n\n}\n}"
to summarize: cljs only does this under strict circumstances: the symbol refers to a const and all test constants are primitives of the same type (numbers or strings). Given this, it seems easy enough to shoot yourself in the foot, but both David and Mike have written about it as if this is a conscious design decision, so I think it's a ship that has sailed. So I'll get rid of a false positive in clj-kondo for this.