This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-22
Channels
- # announcements (1)
- # babashka (94)
- # beginners (11)
- # biff (9)
- # calva (21)
- # clj-kondo (14)
- # clojure (12)
- # clojure-europe (56)
- # clojure-nl (1)
- # clojure-norway (41)
- # clojure-uk (4)
- # clojurescript (4)
- # core-logic (2)
- # gratitude (12)
- # honeysql (1)
- # hoplon (3)
- # hugsql (7)
- # introduce-yourself (2)
- # jobs-discuss (23)
- # leiningen (3)
- # malli (11)
- # off-topic (1)
- # pedestal (11)
- # reagent (3)
- # squint (8)
- # vim (9)
- # xtdb (3)
;; works
(do
(require '[clojure.string :as str5])
(str5/lower-case "FOO"))
;; works
(do
(require '[clojure.string :as str6])
((fn [x] (str6/lower-case x)) "FOO"))
;; throws: No such namespace: str7
((do
(require '[clojure.string :as str7])
(fn [x] (str7/lower-case x)))
"FOO")
;; throws: No such namespace: str8
((fn [x]
(require '[clojure.string :as str8])
(str8/lower-case x))
"FOO")
I was a bit surprised the last two don't work. Is there a way to do a require and use it on the same expr?Top-level do
is special, as I recall, but you might want to look at requiring-resolve
for dynamically requiring and resolving symbols at runtime.
Symbols are resolved at compile time, but require doesn't have a compile time effects, it has a runtime effect
So if a form depends on the effect of a require, the require needs to be compiled and run before that form can be compiled
do is special in that a top level do (not nested in any other expression) is treated as a sequence of top level expressions to be compiled then executed one after the other instead of being treated as a single expression to be compiled and executed all at once
makes sense, thanks for the insight!
((fn [x]
(requiring-resolve 'clojure.string/lower-case)
(clojure.string/lower-case x))
"FOO")
does workThe compiling of the call to lower-case there is relying on the runtime effect of requiring-resolve
So that won't compile unless something has already loaded clojure.string before that form is compiled
It will work in fresh REPL because clojure.string is required during the initialization of clojure.core. Same for http://clojure.java.io. This, for example, won't work:
((fn [x]
(requiring-resolve 'clojure.set/intersection)
(clojure.set/intersection #{} x))
#{})