Skip to content

Instantly share code, notes, and snippets.

@jafingerhut
Last active January 2, 2016 20:49
Show Gist options
  • Save jafingerhut/8359201 to your computer and use it in GitHub Desktop.
Save jafingerhut/8359201 to your computer and use it in GitHub Desktop.
do's inside top level do's are 'flattened'
;; In a fresh Clojure 1.5.1 'lein repl'
;; clojure.edn is not loaded yet
user=> (clojure.edn/read-string "(5)")
ClassNotFoundException clojure.edn java.net.URLClassLoader$1.run (URLClassLoader.java:366)
;; trying to eval this form does not do it, because it fails to analyze as a whole
user=> (eval `(when true (require 'clojure.edn) (clojure.edn/read-string "(5)")))
CompilerException java.lang.ClassNotFoundException: clojure.edn, compiling:(/private/var/folders/n5/pypfx1x96gjgzzdhp0jkxl580000gn/T/form-init1598985253681557129.clj:1:1)
;; Putting it in a do lets each subform be analyzed and evaluated before the next
user=> (eval `(do (require 'clojure.edn) (clojure.edn/read-string "(5)")))
(5)
;; Now clojure.edn is loaded
user=> (clojure.edn/read-string "(5)")
(5)
;;;;;;;;
;; Start over with a fresh 'lein repl' that does not have clojure.edn loaded yet
user=> (clojure.edn/read-string "(5)")
ClassNotFoundException clojure.edn java.net.URLClassLoader$1.run (URLClassLoader.java:366)
;; Now do the require in a do that is inside of a top level do. Still works.
user=> (eval `(do (println "hi") (do (require 'clojure.edn) (clojure.edn/read-string "(5)"))))
hi
(5)
user=> (clojure.edn/read-string "(5)")
(5)
;;;;;;;;
;; Start over with a fresh 'lein repl' that does not have clojure.edn loaded yet to check
;; that you can nest it multiple levels deep. You can.
user=> (clojure.edn/read-string "(5)")
ClassNotFoundException clojure.edn java.net.URLClassLoader$1.run (URLClassLoader.java:366)
user=> (eval `(do (println "hi") (do (println "there") (do (require 'clojure.edn) (clojure.edn/read-string "(5)")))))
hi
there
(5)
user=> (clojure.edn/read-string "(5)")
(5)
;; Link to portion of Clojure compiler source code that handles this:
;; https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L5733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment