Why use a single quote around a vector in "use" (in clojure)?

When I use several long function names, I used the use form, for example:

 (use '[clojure.string :as str]) 

But I donโ€™t know why to add a single quote to the vector ' , so I tried to find out its type:

 (type '[clojure.string :as str]) ;=> clojure.lang.PersistentVector 

A simplified example:

 (type ["hello"]) ;=> clojure.lang.PersistentVector (type '["hello"]) ;=> clojure.lang.PersistentVector 

It seems that a single quote doesnโ€™t change anything, can anyone explain its use in the use form?

+5
source share
2 answers

The goal is to quote characters. Thus, they will be considered characters, and use can accept these characters as namespace names for loading and drawing into the current one. You want to avoid the default processing of a character that resolves it as the name Var and uses the value of that Var. You can also do it like

 (use ['clojure.string :as 'str]) 

but this requires some unnecessary typing; quoting the entire vector allows you least to forget anything. In particular, if you are doing something with :only :refer or similar keyword arguments.

In addition: ns does not need this because, as a macro, it can control the evaluation of its arguments - functions such as require and use have all their arguments read and evaluated before they are run. This is part of the reason ns usually preferred over these functions.

+10
source

use is a function, so the evaluator evaluates his arguments before passing them (evaluating the applicative order).

You do not want [clojure.string :as str] evaluated as an evaluator trying to resolve characters in it without success before using use .

Therefore, a quote (reading transcript ' ) exists to prevent their appreciation.

+3
source

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


All Articles