Why do we need (require "[...]]) with Clojure?

I see that we don’t need 's (ns ...), since ns is a macro. However, why do we need “in (require '[...])? I thought the Clojure vector is a weird way to avoid,” but now I see it here.

We use it (require 'clojure.string), therefore it requireseems to be a function, but we do not give a vector when given as a parameter.

enter image description here

The error message from not quoting is also confusing.

+4
source share
2 answers

This is because it requireis implemented as a function, not a macro, so it needs the libspec quoted. Unsolicited libspec will be evaluated as follows:

user=> [clojure.set :as s]
CompilerException java.lang.ClassNotFoundException: clojure.set

which gives an error.

, ns , , libspec. ns , , :

user=> (use 'clojure.pprint)
nil
user=> (pprint (macroexpand '(ns foo (:require [clojure.set :as s]))))
(do
 (clojure.core/in-ns 'foo)
 (clojure.core/with-loading-context
  (clojure.core/refer 'clojure.core)
  (clojure.core/require '[clojure.set :as s]))
 (if
  (.equals 'foo 'clojure.core)
  nil
  (do
   (clojure.core/dosync
    (clojure.core/commute
     @#'clojure.core/*loaded-libs*
     clojure.core/conj
     'foo))
   nil)))
nil

, ns libspec , , , .

+5

: .

user> (= '[a b c] ['a 'b 'c])
=> true

require , , , .

+2

Source: https://habr.com/ru/post/1614821/


All Articles