Skip to content

Instantly share code, notes, and snippets.

Last active Oct 14, 2020
What would you like to do?
Mixing macros and code in cljc and supporting clj, cljs and self-hosted cljs, see
;; SEE:
;; macros and code in a single cljc working across clj, cljs and self-hosted cljs
;; require clojurescript from master
(ns foo.core
#?(:cljs (:require-macros
[net.cgrand.meta-macros :refer [macros no-macros]]
[foo.core :refer [add]])
:clj (:require
[net.cgrand.meta-macros :refer [macros no-macros]])))
(defmacro add
[a b]
`(+ ~a ~b)))
(defn sum
[a b]
(add a b)))
;; SEE:
(ns net.cgrand.meta-macros)
(defmacro macros [& body]
(when (re-matches #".*\$macros" (name (ns-name *ns*)))
; bootstrapped cljojurescript macro
`(do ~@body))
:clj ; clj or cljs/jvm
(when-not (:ns &env)
`(do ~@body)))) ; clj
(defmacro no-macros [& body]
(when-not (re-matches #".*\$macros" (name (ns-name *ns*)))
`(do ~@body)) ; any cljs non-macro
:clj ; clj or cljs/jvm macro
`(do ~@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment