Cljs case & ^:const

so… i have this silly problem involving case and ^:const in cljs… and in my case i am pretty sure it would be best to simply go with condp = … which works perfectly fine for me… and for me this is clearly no performance bottleneck … so… this morning i wanted to ask about this… then i thought better of it, because premature optimization is the root of all evil… but now i am thinking… well… why not ask if someone has already dealt with the same issue… and has found a simple / clean solution for it… which i could borrow… because even if it doesn’t matter in terms of performance… i still would find it nicer to go with ‘case’ instead of ‘condp =’…

…so … here is the thing…

i use luminus to generate my project template…

lein new luminus case_condp +http-kit +postgres +reagent +auth

i fire up emacs… cider-jack-in-clj&cljs… figwheel…

alright… now… if i use something like:

(def ^:const FOO :FOO)

with something like…

(condp = x
  FOO "match for x = :FOO"
  "default")

there is no problem… even… while i am developing… but when i change that to:

(case x
  FOO "works in prod... but not in dev... for x = :FOO"
  "default")

this works just fine after i push to production… but in dev… this stops to work for me… and so i end up with the default clause…

any ideas?

Your text is very hard to read with “…” scattered throughout it. I think what you’re running into is the fact that case does not resolve symbols used in the match on the left hand side. So:

(def FOO "bar")

(case FOO
  FOO "baz"
  "default")
;; => default

Like you said, since it will try and match the value “baz” to the symbol FOO.

(def XYZ 'FOO)

(case XYZ
  FOO "baz"
  "default")
;; => "baz"

So for your case, you should continue to use condp = if you want to store the value you want to match against inside of a var.

1 Like

thx for answering so quickly!.. also… reading the dots is just like reading s-exp… it just takes some time to get used to… but then it really is great fun :smile:

This is a trickier situation then what @lilactown described. The ^:const will mean that on compilation of the code, all instances of the symbol FOO will be replaced by the value of the Var. This explains why it works for you in production. I’m guessing there’s something in your dev environment which bypasses that step from the compiler. I don’t know enough of the details though to make a guess.

2 Likes