什么';s习惯用语clojure for:use

什么';s习惯用语clojure for:use,clojure,idioms,Clojure,Idioms,我见过几种不同的:在clojure中使用——惯用/首选的方法是什么 #一, 或#2编辑:与:仅一起使用 (ns namespace.core (:use [something.core] [another.core])) 或#3 或#4 或者#5编辑:这是惯用的,但应该使用:使用,如#2所示 案例1、3和4无效,并引发一些异常。我没有看到2-仅与组合:仅与或类似的组合 (ns namespace.core (:use [something.core :only

我见过几种不同的
:在clojure中使用
——惯用/首选的方法是什么

#一,

或#2编辑:与
:仅
一起使用

(ns namespace.core
  (:use [something.core]
        [another.core]))
或#3

或#4

或者#5编辑:这是惯用的,但应该使用
:使用
,如#2所示


案例1、3和4无效,并引发一些异常。我没有看到2-仅与
组合:仅与
或类似的组合

(ns namespace.core
  (:use
    [something.core :only (x)]
    another.core))
我通常使用5。

选择#5是惯用的,除非您传递附加选项,如:only、:exclude等。详细介绍了这些选项


处理名称空间的API不必要地难以学习。然而,它确实有足够的能力用于各种各样的用途,因此重写的压力还没有达到任何人都能承受的极限。

事实上,没有一个是惯用的。在:uses中应该始终有一个:only子句。你最好的选择是加上:只加2。如果您不想枚举您从另一个命名空间中提取的所有VAR,请考虑(:需要[Fo.bar:AS Bar)]。 值得一提的是 (:use(clojure set xml))语句被认为是一个混乱的操作 因此感到沮丧。[…]在组织您的 沿着名称空间编码,最好只导出和导入这些名称空间 需要的要素

-摘自《Clojure的欢乐》,第183页


一个例外是测试名称空间应该使用它测试的名称空间。

在Clojure 1.4+中,我根本不会使用
use
require
可以做
use
现在能做的一切,所以忘记
use
。少担心一件事

如果您想
使用类似于
的行为(imo,形式仍然很糟糕),您可以:

(ns namespace.core
  (:require
    [something.core :refer :all]
    [another.core :refer :all]))
如果需要
:请使用..:仅限行为,请使用:

(ns namespace.core
  (:require
    [something.core :refer [foo bar]]
    [another.core :refer [quux]]))

更多细节:

Oops,我正在看我的项目。clj:dependencies to get#1。
(ns namespace.core
  (:use something.core
        another.core))
(ns namespace.core
  (:use
    [something.core :only (x)]
    another.core))
(ns namespace.core
  (:require
    [something.core :refer :all]
    [another.core :refer :all]))
(ns namespace.core
  (:require
    [something.core :refer [foo bar]]
    [another.core :refer [quux]]))