Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 是否:参考和:同时使用?_Clojure_Namespaces_Alias_Clojurescript - Fatal编程技术网

Clojure 是否:参考和:同时使用?

Clojure 是否:参考和:同时使用?,clojure,namespaces,alias,clojurescript,Clojure,Namespaces,Alias,Clojurescript,当我阅读Clojure代码(比如GitHub上的各种项目)时,有一点我不太理解:在:require表达式中,当您仅使用所指的函数时,是否应该使用:as?此外,从这个角度来看,我也不太确定Clojure和ClojureScript之间是否有任何区别,也不确定是否有任何惯例 例如(我正在使用cats库进行示例,但这只是一个示例),如果我这样做: (ns example.core (:require [cats.core] [cats.monad.maybe :as maybe

当我阅读Clojure代码(比如GitHub上的各种项目)时,有一点我不太理解:在
:require
表达式中,当您仅使用所指的函数时,是否应该使用
:as
?此外,从这个角度来看,我也不太确定Clojure和ClojureScript之间是否有任何区别,也不确定是否有任何惯例

例如(我正在使用cats库进行示例,但这只是一个示例),如果我这样做:

(ns example.core
  (:require
     [cats.core]
     [cats.monad.maybe :as maybe]))
例如,从REPL,我可以这样做:

REPL> (cats.core/mappend (maybe/just 1))
但这不是很方便,所以我可以这样做:

(ns example.core
  (:require
     [cats.core :as m]
     [cats.monad.maybe :as maybe]))

REPL> (m/mappend (maybe/just 1))
现在,如果我一直使用
mappend
(以及其他一些),我可以:

(ns example.core
  (:require
     [cats.core :refer [mappend]]
     [cats.monad.maybe :as maybe]))

REPL> (mappend (maybe/just 1))
所以我的问题是:同时使用
:as
:refere
,如下面的示例中所示,有什么用处吗

(ns example.core
  (:require
     [cats.core :as m :refer [mappend]] ; does this make any sense?
     [cats.monad.maybe :as maybe]))
我知道我可能一直在使用,比如说,
mappend
,可能想直接将其称为
mappend
,但
bind
的使用频率不如
:refered
的使用频率高,但是编写
cats.core/bind
并不方便,所以我会使用
m/bind

有什么规定吗?例如,如果您使用的是
:as
您不应该使用
:请参考
,或者反过来?还是说你应该两个都用


ClojureScript呢,它的工作方式完全相同吗?

您在用例上是对的;您经常使用的东西,您可能希望直接引用,否则,只需使用
:as
别名即可访问。两者都做没有问题

我个人倾向于选择
:as
,除非非常清楚
:reference
ed函数来自哪个名称空间(例如,我会识别
!!
而不带前缀,但发现
m/bind
更具可读性)

afaik最广泛使用的样式指南建议结合使用它们是好的,但在大多数情况下,更喜欢
:as
而不是
:reference


Clojurescript对于
:reference
:与
ns

中的
完全相同是的,有时您确实希望同时使用这两种脚本,例如,当您决定使用不带名称空间前缀的core.async“primitives”(
:refere[!]
)时,但需要避免与Clojure core冲突(
async/into
)@glts:ah这一切都很有趣。而且我没有想到冲突:很有意义。谢谢你们两位。