Will it ever be possible to use reader conditionals in clojure macros

224 views
Skip to first unread message

Arewa Olakunle

unread,
Mar 17, 2017, 8:37:27 AM3/17/17
to Clojure
I have been trying to define a macro for clojurescript/clojure using  a cljc file

(ns async
  (:require #?(:clj [clojure.core.async :as a :refer [go-loop]])
            #?@(:cljs [[cljs.core.async :as a]
                      [cljs.core.async.macros :refer [go-loop]]])))

(defmacro dochan
  "Wrap the expressions in a go-loop over the single binding to a channel
  till the chan closes"
  [chan & body]
  (let [[name channel-expr] binding]
    `(let [channel# ~channel-expr]
       (a/go-loop [~name (a/<! channel#)]
         (if (some? ~name)
           (do
             ~@body
             (recur (a/<! channel#)))
           (a/close! channel#))))))
;; in CLJS
(ns cljs
  (:require [cljs.core.async :as a]))

(dochan [e (a/chan 10)]
  (println "Auto-insert" e)) ;; keeps throwing exception saying it cannot find clojure.core.async


Further macroexpansion in cljs-repl confirmed what I was saying with the resulting code using clojure.core.async. So I came to the conclusion that since clojurescript relies on clojure for macros, it would use clojure to load the macros , hence making the reader conditionals work like the macro's to be used in a clojure file. Another weird thing happened though. It was auto-inserting clojure.core.async whenever I remove the :clj require. I am guessing it's cider because whenever I started a different repl it would behave normally, using a alone rather than cojure.core.asyn

Arewa Olakunle

unread,
Mar 17, 2017, 12:30:36 PM3/17/17
to Clojure
Sorry I posted this here, wasn't aware of clojurescript group. Found a similar posting(almost a clone) here with a useful hack here
Reply all
Reply to author
Forward
0 new messages