Clojure中use和require的区别

29
我最近开始学习Clojure,但我有些难以理解命名空间。正如Clojure的创建者所说,新手经常在概念上遇到困难。我不清楚(use ...)(require ...)之间的区别。例如,如果我在REPL中尝试(use 'clojure.contrib.str-utils2),就会收到关于将clojure.core命名空间中的函数替换为clojure.contrib.str-utils2中的函数的警告,但是当我使用(require 'clojure.contrib.str-utils2)时,这种情况并不会发生。我不确定我是否总是想要替换clojure.core中的内容,所以有没有人可以指出导入外部内容和管理Clojure命名空间的一些最佳实践呢?
还有,什么时候应该使用:use:require?只有在(ns ....)内部吗?
提前感谢您的回答。

可能是difference between use and require的重复问题。 - om-nom-nom
1个回答

44

答案在文档字符串中:

user> (doc use)
-------------------------
clojure.core/use
([& args])
  Like 'require, but also refers to each lib's namespace using
  clojure.core/refer. Use :use in the ns macro in preference to calling
  this directly.

  'use accepts additional options in libspecs: :exclude, :only, :rename.
  The arguments and semantics for :exclude, :only, and :rename are the same
  as those documented for clojure.core/refer.
nil

对于 require,需要更长的代码:

user> (doc require)
-------------------------
clojure.core/require
([& args])
  Loads libs, skipping any that are already loaded. Each argument is
  either a libspec that identifies a lib, a prefix list that identifies
  multiple libs whose names share a common prefix, or a flag that modifies
  how all the identified libs are loaded. Use :require in the ns macro
  in preference to calling this directly.

  Libs

  A 'lib' is a named set of resources in classpath whose contents define a
  library of Clojure code. Lib names are symbols and each lib is associated
  with a Clojure namespace and a Java package that share its name. A lib's
  name also locates its root directory within classpath using Java's
  package name to classpath-relative path mapping. All resources in a lib
  should be contained in the directory structure under its root directory.
  All definitions a lib makes should be in its associated namespace.

  'require loads a lib by loading its root resource. The root resource path
  is derived from the lib name in the following manner:
  Consider a lib named by the symbol 'x.y.z; it has the root directory
  <classpath>/x/y/, and its root resource is <classpath>/x/y/z.clj. The root
  resource should contain code to create the lib's namespace (usually by using
  the ns macro) and load any additional lib resources.

  Libspecs

  A libspec is a lib name or a vector containing a lib name followed by
  options expressed as sequential keywords and arguments.

  Recognized options: :as
  :as takes a symbol as its argument and makes that symbol an alias to the
    lib's namespace in the current namespace.

  Prefix Lists

  It's common for Clojure code to depend on several libs whose names have
  the same prefix. When specifying libs, prefix lists can be used to reduce
  repetition. A prefix list contains the shared prefix followed by libspecs
  with the shared prefix removed from the lib names. After removing the
  prefix, the names that remain must not contain any periods.

  Flags

  A flag is a keyword.
  Recognized flags: :reload, :reload-all, :verbose
  :reload forces loading of all the identified libs even if they are
    already loaded
  :reload-all implies :reload and also forces loading of all libs that the
    identified libs directly or indirectly load via require or use
  :verbose triggers printing information about each load, alias, and refer

  Example:

  The following would load the libraries clojure.zip and clojure.set
  abbreviated as 's'.

  (require '(clojure zip [set :as s]))
nil

这两个函数的作用相同,但是use会额外执行一步操作,并在当前命名空间中为require的命名空间中的内容创建映射。这样,你不需要像some.namespace/name这样引用,而可以直接使用name。虽然有时候这很方便,但最好还是使用require或者选择想要的单个变量,而不是引入整个命名空间。否则,可能会出现遮蔽问题(即同名变量之间的优先级)。

如果您不想使用require,但您知道您想从命名空间中获取哪些变量,可以这样做:

(ns whatever
  (:use [some.namespace :only [vars you want]]))

如果你不知道你需要哪些变量,或者需要很多变量,最好使用require。即使你使用require,也不一定需要输入完全限定的名称。你可以这样做:

(ns whatever
  (:require [some.namespace :as sn]))

然后你可以像这样使用来自some.namespace的vars:(sn/somefunction arg1 arg2)

回答你的最后一个问题:尽量只在(ns ...)内使用:require和:use。这样做更加清晰。除非你有充分的理由,否则不要在(ns ...)之外使用userequire


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接