lc2817
lc2817

Reputation: 3742

Clojurescript macro and name mangling

Context

I am using Clojurescript and I am trying to define a lot of functions at compile time to wrap a Javascript API. My code works fine with lower level of optimization of the compiler. Yes, when I use the :optimizations :advanced Clojurescript compiler flag, the compiler throws warning: Use of undeclared Var my.namespace/fname and my code does not work at runtime (some mangled symbol not found).

Here is a minimum example of the issue:

(defmacro create-a-function [l]
  `(defn ~l [o#] (inc o#)))

(create-a-function fname)

;; Below inside another function
(defn fname-clone [k]
  (fname k))

I decide a function called fname at compile-time using the macro create-a-function. When I try to call the function at runtime it fails.

What have I tried?

My question

How can I make this kind of code work with advanced level of optimization?

Upvotes: 3

Views: 752

Answers (1)

Michiel Borkent
Michiel Borkent

Reputation: 34800

I reproduced your code in this repo and it works.

Note that you have to put your macro in a .clj file.

When you want to use macros from a .cljc file in ClojureScript, use macrovich.

Upvotes: 3

Related Questions