Last active
October 14, 2020 12:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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