Fork me on GitHub
#core-async
<
2018-03-25
>
wilkerlucio00:03:59

I'm using core.async trying to create a macron a cljc file like this:

wilkerlucio00:03:01

(defmacro go-catch [& body]
  `(async/go
     (try
       ~@body
       (catch #?(:clj Throwable :cljs :default) e# e#))))

wilkerlucio00:03:26

the macro works fine on clojure, but on cljs every time I try to run it I get: java is not defined, you know what that's about?

mfikes01:03:33

@wilkerlucio If you load that macro using JVM ClojureScript, then the macro is being compiled as Clojure, thus taking the :clj branch and putting java.lang.Throwable into your ClojureScript code, I would guess.

wilkerlucio01:03:32

humm, do you have an example on how should I handle this? I have to make a separated version for cljs?

mfikes01:03:42

This is the if-cljs stuff.. where you detect if you macro is being expanded for use in ClojureScript.

wilkerlucio01:03:55

trying to find some examples for it, but google is not helping me much... do you know some place where I can see that in use?

wilkerlucio01:03:32

nevermind, just found a thread with it 🙂

wilkerlucio01:03:16

but that still doesn't seem to work in my case =/

mfikes01:03:48

Did your approach involve peeking in &env?

wilkerlucio01:03:52

you mean the if-cljs?

mfikes01:03:13

By the way, vote for https://dev.clojure.org/jira/browse/CLJ-1293 as it would provide a portable solution 🙂

mfikes01:03:50

yeah, you said it doesn't work. Wondering what you tried

wilkerlucio01:03:37

sure, I tried this:

wilkerlucio01:03:39

(defmacro if-cljs
  [then else]
  (if (:ns &env) then else))

(defmacro go-catch [& body]
  `(async/go
     (try
       ~@body
       (catch (if-cljs :default Throwable) e# e#))))

wilkerlucio01:03:29

and just voted, this error handling is surely something would be nice to have portable

mfikes01:03:58

I think you need to unquote your if-cljs form

mfikes01:03:02

(I could be wrong... let me look up how to use that thing again.)

wilkerlucio01:03:59

unquoting seems to break the compilation

mfikes01:03:10

@wilkerlucio Right, your code above appears to be correct.

wilkerlucio02:03:53

well, I got it working, but I had to duplicate the code across 2 different namespaces

wilkerlucio02:03:20

I made async-clj and async-cljs, and the consumers now require like this:

[#?(:clj  com.wsscode.common.async-clj
        :cljs com.wsscode.common.async-cljs) :refer [go-catch]]

wilkerlucio02:03:23

not ideal, but works, when that jira gets in will be awesome