Fork me on GitHub
#clojure-europe
<
2020-07-29
>
David Pham07:07:54

Morning 🙂

plexus08:07:00

morning from the :flag-be: side

plexus08:07:12

nice to be out of berlin for a change

genRaiy10:07:54

@otfrom are you still stumped?

otfrom12:07:31

life (and a big machine) have just ground that stump down

Ben Hammond10:07:25

is it like the ending of Fantastic Mr Fox?

RAMart12:07:32

Hi all! Please forgive me, this is a somewhat lengthy question: We have a rather big Clojure/ClojureScript cross-compiling project which is great and fun and runs smooth as silk. I'm really happy with it. Of course we use some – not a lot – macros. If you setup a namespace like this:

(ns your.namespace.with.macros
  #?(:cljs (:require-macros [your.namespace.with.macros])))

#?(:clj
(defmacro your-awesome-magic
  ;; ...
  ))
in a .cljc file, the macro will be exposed and will be available. So you can require that namespace in Clojure and ClojureScript. This is because of evil magic called Implicit macro loading (see https://clojurescript.org/about/differences#_namespaces for details). If you change it a little:
(ns your.namespace.with.macros
  #?(:cljs (:require-macros [your.namespace.with.macros])))

(defn macro-helper
  []
  ;; ....
  )

#?(:clj
(defmacro your-awesome-magic
  ;; ...
  '(macro-helper)
  ))
it stays the same, but macro-helper will also be exposed and available when requiring that namespace. But be aware that there is something subtle going on: For ClojureScript, this file get's compiled twice: Once for the Clojure world, because this is where macros live, and once more for the ClojureScript world. That means that macro-helper will also be compiled in Clojure as well as in ClojureScript, although – in this case – it's not needed in the Clojure world, because it's not needed by the code run by the macro. It's needed later by the code produced by the macro. So macro-helper is compiled by Clojure in the macro compilation phase but never needed. Not a big problem. Except when it is: In our case, macro-helper can not be compiled by the Clojure compiler in the macro compilation phase when targeting ClojureScript. It can be compiled when targeting Clojure. That is because of the source paths and library setups. So the reader macros for :clj and :cljs won't help. The reader macro I need would be :clj-except-when-compiling-macros-for-cljs or something like that. The only solution I found: Move macro-helper to another namespace and require both. It works, of course, but I don't like it. If there is the slightest chance that I was able to explain the problem, any help would be much appreciated. And sorry for the lengthy post.

borkdude12:07:39

@ramart I think I'm using macrovich to address that problem

6
RAMart12:07:52

By the Grande cgrand? I'll have a look at it. Thanks! (Also for [clj-kondo), babashka, sci and so much more! 😍)

borkdude12:07:39

haha, welcome. And thanks for organizing ClojureD, I'm glad we got to visit it just before the virus thing happened

🍻 3
ordnungswidrig12:07:00

Why does it harm that macro-helper is compiled for clj-only code?

RAMart12:07:56

Because it's required by the code produced by the macro and must therefore be available in the ClojureScript world.

RAMart12:07:41

But @U04V15CAJ pointed me to the right direction. And it's also a good opportunity to write Christophe a mail. 😀

RAMart14:07:17

Yes, thanks. The major difference is that we also target Clojure. So the mentioned inverse* function of the linked article needs to be available in Clojure and ClojureScript but not during macro compilation, because it does not compile then. But https://github.com/cgrand/macrovich exactly solves that problem. > Because any macros problem can be solved by another level of macros – Christophe Grand