Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an if-clojurescript macro?

Is there a macro for Clojure and ClojureScript that would allow you to insert different expressions depending on whether the file is being compiled in Clojure or Clojurescript?

(if-clojurescript
   (my clojurescript definition)
   (my clojure definition))

Essentially I'm looking for something like the #ifdef SOME_PLATFORM macros you might see sprinkled around C/C++ code. I think it could be useful for files that I would like to be part of a cross-over, but for which one small part of that file isn't compatible between Clojure/ClojureScript.

like image 468
Rob Lachlan Avatar asked Mar 01 '13 18:03

Rob Lachlan


2 Answers

This is a new feature from Clojure 1.7 : Reader conditionals.

Also see Daniel Compton blog, for example :

#?(:clj  (Clojure expression)
   :cljs (ClojureScript expression)
   :clr  (Clojure CLR expression))

You may also want to look at macrovitch.

like image 159
nha Avatar answered Oct 10 '22 18:10

nha


you could check *clojure-version*

user> (if *clojure-version* "I'm Clojure" "I'm ClojureScript")
"I'm Clojure" 

cljs.user> (if *clojure-version* "I'm Clojure" "I'm ClojureScript")
"I'm ClojureScript"

This could be useful in cases where you can't split the language neutral bits into their own file (which is preferable). My personal opinion tends towards avoiding using too many of such things.

 (defmacro if-clojurescript [clj-form cljs-form]
    (if *clojure-version* clj-form cljs-form))
like image 3
Arthur Ulfeldt Avatar answered Oct 10 '22 17:10

Arthur Ulfeldt