Fork me on GitHub
#clojurescript
<
2021-12-30
>
Chris McCormick13:12:51

Is there a way to do if ("ontouchend" in document) in ClojureScript? The in keyword I mean.

paulocuneo13:12:21

;; If missing and nil means the same
(let[ doc {"str-key" "str-val" 
            :key1 :val1 
            :key2 nil}] 
      (if (doc "str-key") 
         :key-found
         :not-found))

(let[ doc {"str-key" "str-val" 
           :key1 :val1 
           :key2 nil}] 
      (if (doc :key2) 
          :key-found
          :not-found))
;; if missing and nil don't mean, personally I would avoid it
(let[doc {"str-key" "str-val" 
          :key1 :val1 
          :key2 nil}] 
     (if (not= ::not-found (doc :key2 ::not-found)) 
        :key-found
         :not-found))

lukas.rychtecky13:12:35

I think this should work:

(goog.object/containsKey js/document "ontouchend")
https://google.github.io/closure-library/api/goog.object.html#containsKey

thheller17:12:31

(js-in "ontouchend" js/document)

💯 1
dvingo13:12:56

I am having trouble getting a basic tagged literal working in ClojureSript. I put together a small reproduction here (replicated with shadow and the cljs compiler): https://github.com/dvingo/data-readers-bug-cljs it works fine with clojure - is there something obvious I'm missing?

lilactown16:12:10

ClojureScript reader tags are processed in Clojure. your read-it fn isn't available in Clojure, it's ClojureScript

lilactown16:12:10

basically, the CLJS compiler is a Clojure program. when it reads your CLJS code, it looks up tagged literals that are registered and attempts to call the Clojure function associated with it. In this case, there is no Clojure function called hello-world.core/read-it so it fails to compile

lilactown16:12:02

you can fix your example by creating a hello_world/core.clj file with the read-it fn, and then using :require-macros to make sure it's included while processing your hello_world/core.cljs file

lilactown16:12:08

in more complicated cases, reader tags used with compiled CLJS should emit CLJS code (like a macro) since the reader tags are executed at compile time. this is different than CLJ

dvingo17:12:59

hey thanks for the detailed explanation - I got things working for plain cljs - but shadow is still failing (updated the repo)

dvingo17:12:29

ok - I think I figured it out for shadow-cljs as well - thanks for the help again!