Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access compile time cljs var metadata from a clj macro?

I'd like to construct a ClojureScript macro (executed/compiled via Clojure) that informs it's construction of a return form based on the static, compile-time metadata of a ClojureScript var argument.

I understand it is possible to access compile-time, static var metadata from ClojureScript code (using (meta (var varsym)); see this post). But is this data accessible to the compilation process in such a way that we could access it from a macro?

Here's a rough sketch of what I'd like to do (and the question is really how you'd write get-meta-for-varsym below):

;; executed/compiled in clj, targeting cljs
(defmacro themacro
  [varsym & args]
  (let [var-meta (get-meta-for-varsym varsym)
        return-form (compile-return-form-from-metadata var-meta args)]
    return-form))
like image 531
metasoarous Avatar asked May 06 '16 17:05

metasoarous


1 Answers

For this you have to use the Clojurescript analyzer:

(ns your-macros
  (:require [cljs.analyzer :as cljs]))

(defmacro var-data
  [sym]
  (cljs/resolve-var &env sym))

Then in your clojurescript file:

(ns your-cljs)

(def ^{:foo :bar} xxy {})

(var-data xxy)

The meta data will be in :meta key of the map.

like image 67
ClojureMostly Avatar answered Sep 20 '22 17:09

ClojureMostly