Skip to content

Instantly share code, notes, and snippets.

@cgrand
Last active October 14, 2020 12:18
Show Gist options
  • Save cgrand/c1a83d8bb5fc0b5a6c53e5a6deece213 to your computer and use it in GitHub Desktop.
Save cgrand/c1a83d8bb5fc0b5a6c53e5a6deece213 to your computer and use it in GitHub Desktop.
Mixing macros and code in cljc and supporting clj, cljs and self-hosted cljs, see https://github.com/cgrand/macrovich
;; SEE: https://github.com/cgrand/macrovich
;; 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]])))
(macros
(defmacro add
[a b]
`(+ ~a ~b)))
(no-macros
(defn sum
[a b]
(add a b)))
;; SEE: https://github.com/cgrand/macrovich
(ns net.cgrand.meta-macros)
(defmacro macros [& body]
#?(:cljs
(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]
#?(:cljs
(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