Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to use slurp from ClojureScript
(ns foo.core
(:refer-clojure :exclude [slurp]))
(defmacro slurp [file]
(clojure.core/slurp file))
;; In CLJS
(ns bar.core
(:require [foo.core :include-macros true :refer [slurp]]))
;; This is possible because we can evaluate *Clojure* code at compile time.
(def project-clj
(slurp "project.clj"))

This comment has been minimized.

Copy link

@FrankApiyo FrankApiyo commented Mar 24, 2020

I haven't tried this but I'm skeptical because it does not make any sense to me 😞


This comment has been minimized.

Copy link
Owner Author

@noprompt noprompt commented Mar 24, 2020

@FrankApiyo ClojureScript uses Clojure to expand macros. This means the code inside the slurp macro above is being invoked in the Clojure environment and, thus, all of the usual Clojure functions are available. In this case, slurp will return the content of project.clj as a string and "inline" it on line 14.

So if project.clj looked like

(defproject foo)

then line 13 to 14 expands to

(def project-clj
  "(defproject foo)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.