Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Repl compiling Clojure
(defn compile-inline [ns-name code]
(let [tmp-dir *compile-path*
sanitized-ns-name (.replace (str ns-name) "-" "_")
clj-file ( (str tmp-dir "/" (clojure.string/join "/" (clojure.string/split sanitized-ns-name #"\.")) ".clj"))]
( clj-file)
(with-open [file ( clj-file)]
(binding [*out* file]
(str "(ns " ns-name ")\n"
(binding [*compile-path* (str tmp-dir)] ;; str here is important, the compile doesn't like something else
(compile (symbol ns-name))
(catch e
;; *compile-path* also needs to be on the classpath! (how can we check this?!
(throw (ex-info (str "*compile-path* : " (pr-str (str tmp-dir)) " isn't on the classpath")
{:compile-path *compile-path*
:clj-file clj-file}
#_e ;; If we add the cause, the above error doesn't show directly
{:clj-file clj-file
:ns-name ns-name}))
(defn pp-str [x]
(with-out-str (clojure.pprint/pprint x)))
(defmacro script
[& lines]
(let [lines-str (map pp-str lines)]
`(clojure.string/join "\n" [~@lines-str])))
(compile-inline 'foo
:name "some.package.RefMap5"
;:implements [java.util.Map]
:state "state"
:init "init"
:constructors {[] []}
:prefix "ref-map-")
(defn ref-map-init [])))
;; Get an instance of our fresh class!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment