Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can cljc single-file macro definitions to work with clojurescript?

I have clojurescript successfully importing macros from other namespaces. But I wonder whether a single-file construction is/should be possible with clojure 1.7, such that a macro can be defined and used. What I have tried does not work, but maybe I've missed a detail someplace.

(ns cljc.core)

#?(:cljs
(enable-console-print!))

#?(:clj
(defmacro list-macro [x y]
  `(list ~x ~y)))

(defn foo [a]
  (println (list-macro a a)))

(foo :a)

This form fails with list-macro being undefined when compiling cljs; if I remove the :clj guard around list-macro, then defmacro is undefined within the cljs compilation. Is there a way?

like image 436
ben Avatar asked Apr 28 '15 09:04

ben


People also ask

Is ClojureScript the same as Clojure?

The rationale for ClojureScript is much the same as for Clojure, with JavaScript in the role of platform, and additional emphasis on the reach of JS, as it is obviously not as rich a platform. A deeper discussion on ClojureScript's rationale can be found elsewhere on this site.

What is a macro in Clojure?

Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.

What is Cljc?

CLJC. Church of The Lord Jesus Christ.


Video Answer


1 Answers

Yes, there is a way for a single file construction.

(ns cljc.core
  #?(:cljs (:require-macros [cljc.core :refer [list-macro]])))

#?(:clj
(defmacro list-macro [x y]
;; ...

Assumedly one of the next CLJS compiler versions will do the import automatically.

like image 140
Leon Grapenthin Avatar answered Oct 16 '22 11:10

Leon Grapenthin