Fork me on GitHub
Yehonathan Sharvit06:07:23

I need help with macros. I’d like to create a macro defnmy similar to `defn` but that creates a function whose name is transformed by a function supplied to the macro. Example of usage:

(defnmy FOO clojure.string/lower-case [x]
    (inc x))
It should create a function named `foo` I was able to implement it using `resolve`:
(defmacro defnmy [name fn args body]
  (let [n (symbol ((resolve fn) name))]
    `(defn ~n ~args ~body)))
I would like the macro to workd on self-hosted cljs. The problem is that in cljs resolve is a macro and it works only on non-namespaced symbols. Help appreciated


> resolve is a macro and it only works on non-namespaced symbols

(ins)user=> (resolve ')
(ins)user=> (map resolve '[+ - inc dec])
(#'clojure.core/+ #'clojure.core/- #'clojure.core/inc #'clojure.core/dec)
it is not a macro, and it works on both namespaced and non-namespaced symbols


the problem is that cljs doesn't have real vars, and resolve only works on vars

Yehonathan Sharvit06:07:04

The context of my request is that I am trying to make this library self-host compatible and it has this `defconversion` macro


Did you find any disadvantage of using vs raw JavaScript? This is library for google chrome extension. I have no experience with this, but I need to write something and trying to decide if do this with cljs or maybe in this case with js.


@viebel I'd do it this way and, for the self-hosted case copy enough of the Planck code that bottoms out in the use of eval to pull it off.


An issue might arise if the code referenced in the function itself needs to be loaded; then there would be asynchronous issues to cope with.

Yehonathan Sharvit16:07:44

@mfikes your gist is awesome! Thanks

Yehonathan Sharvit16:07:41

But it makes me asking myself what would be the meaning of making a lib like self-host compatible. One option is to make the lib works fine with planck. In that case, I can use planck.repl/requiring-resolve But then the lib would not work on other self hosted env (lumo, klipse). Can you think of another option?


Copy enough of Planck’s implementation of requiring-resolve into the lib—it is pure ClojureScript that bottoms out on eval

Yehonathan Sharvit16:07:50

What state would I pass to cljs/eval as first arg?


It bottoms out in cljs.core/eval (not cljs.js/eval) 🙂

Yehonathan Sharvit16:07:53

@mfikes Do I need resolve from planck or I can use resolve from cljs?


Oh, yeah, you’d need to copy Plank’s. And that involves cljs.js/eval

Yehonathan Sharvit16:07:18

But I can replace cljs.js/eval by cljs.core/eval.Right?


Well, they’re not directly interchangeable if that’s what you mean

Yehonathan Sharvit16:07:37

No. I meant including code adaptation (not verbatim copy/paste)

Yehonathan Sharvit16:07:54

(defn ns-resolve
  (binding [ana/*cljs-warnings* (zipmap (keys ana/*cljs-warnings*) (repeat false))]
    (eval `(~'var ~sym))))


That should be cool


Perhaps that leaves open the question of which ns the eval occurs in...

Yehonathan Sharvit16:07:36

If the symbol is fully qualified, it should not matter. Right?


Yeah, for this use case it might be cool

Yehonathan Sharvit16:07:21

So what’s the point of requiring-resolve vs resolve?

Yehonathan Sharvit16:07:52

Does planck have a magic trick to run eval synchronously?


This all surrounds whether the namespace for the function symbol passed to defmy has been required or not. If not, then resolve is going to return nil. The default implementation of eval for self-hosted environments is assuming the result of the evaluation is available synchronously. (See But if you evaluate a require form, then it is up to each self-hosted environment to implement that. In Planck's case require does indeed have a synchronous implementation.


(Actually it used to be the case that require needed to be directly implemented by a self-hosted environment, but at some point a macro was introduced, but nevertheless the root issue remains regarding whether cljs.js/*load-fn* is asynchronous.)


Broadly speaking, you want to write a macro that, upon macroexpansion, makes use of a function that may or may not have been loaded at that time. And in self-hosted ClojureScript, the namespace loading mechanism is fundamentally asynchronous.


If your use case knows a priori that the function symbols being passed to defmy are already loaded, then resolve should be sufficient for that use case.


No magic. wizard


^ In the above, resolve is your custom implementation, not cljs.core/resolve

Yehonathan Sharvit17:07:38

Wow! Thanks for this crystal clear explanation


@viebel If you assume the fn has been loaded maybe you can just dispense with all of the complexity and just use eval and var

(defmacro defnmy [name fn args & body]
  (let [n (symbol ((eval `(~'var ~fn)) (str name)))]
    `(defn ~n ~args [email protected])))

Yehonathan Sharvit18:07:43

I was just thinking about that @mfikes What about setting the cljs-warnings to false like in

(binding [ana/*cljs-warnings* (zipmap (keys ana/*cljs-warnings*) (repeat false))]
    (eval `(~'var ~sym)))


I wonder if there are actually any warnings you would want to suppress... the cool thing about avoiding that is that the definition above based on just eval and var also works in Clojure (and thus is perfectly fine as a macro for JVM ClojureScript)

Yehonathan Sharvit18:07:23

Is eval and var preferable over clojure.core/resolve for Clojure and JVM ClojureScript?

Yehonathan Sharvit18:07:49

Or at least not worse


That's a good question. You could use a reader conditional and use clojure.core/resolve if in :clj , but I guess you are pondering if there is a reason to do that. 🙂

Yehonathan Sharvit18:07:45

Yeah. That’s my question


is this the world's biggest yakshave to get camel-snake-kebab working in a self-hosted environment?

Yehonathan Sharvit18:07:27

is (resolve sym) (better than (eval ('var sym))` ?


The yak is very hairy 🙂


I haven't read all the scrollback, but that library doesn't seem to do something that is fundamentally incompatible with any environment


(as in: it's for converting cases of strings)


is there a better value for :args for a zero-arity function than (s/cat) ?

Yehonathan Sharvit18:07:26

@ghadi I am not familiar with the yakshave expression. What does it mean?


> Yak shaving refers to a task, that leads you to perform another related task and so on, and so on — all distracting you from your original goal. This is sometimes called “going down the rabbit hole.”


Sometimes the yak shave is so lengthy, a day has gone by, and you have forgotten what you were actually trying to do.


I imagine camel-snake-kebab can't be more than 40 lines of code


but I have often been wrong 🙂


One aspect that appears to be clouding things is the dynamic ability to pass and resolve arbitrary functions. But the functions that transform function names are limited in scope right?


Ahh, yeah, it feels like this "higher order" macro is nifty, but maybe is also causing the challenges in porting to self-hosted.

Yehonathan Sharvit18:07:13

When defconversion is used inside camel-snake-kebab, the scope is indeed limited.

Yehonathan Sharvit18:07:15

nifty or too nifty (i.e. abuse)?


Oh, sorry by "nifty", I mean "clever"

Yehonathan Sharvit18:07:11

This is what I thought

Yehonathan Sharvit18:07:24

My question: is it too clever?

Yehonathan Sharvit18:07:37

Beign too clever makes sometimes the code hard to decode

Yehonathan Sharvit18:07:46

@ghadi camel-snake-case has 162 lines of code

Yehonathan Sharvit18:07:47

Language                     files          blank        comment           code
ClojureC                         6             31              8            162
SUM:                             6             31              8            162