[英]Clojure{Script} protocol naming
What is the idiomatic way to name a Clojure protocol ? 命名Clojure协议的惯用方式是什么?
IProtocol
or Protocol
, or do they present different use cases, and why ? IProtocol
或Protocol
,还是它们呈现不同的用例,为什么?
So far I use IProtocol
when dealing with interoperability (Java or js/google-closure in ClojureScript). 到目前为止,我在处理互操作性时使用
IProtocol
( IProtocol
Java或js / google-closure)。
I am also wondering about the naming convention of the method names : -my-method
vs my-method
. 我也想知道方法名称的命名约定:
-my-method
与my-method
。 I have seen both in the wild (in ClojureScript codebase), and could not come up with a rationale to explain when use one or the other. 我已经在野外(在ClojureScript代码库中)看到了两者,并且无法提出解释使用一种或另一种的理由。
So, what is the idiomatic way to name abstractions in Clojure and ClojureScript ? 那么,用Clojure和ClojureScript命名抽象的惯用方式是什么?
Yes, this is a way of how you allow java
to call clojure
code. 是的,这是允许
java
调用clojure
代码的一种方式。
If you create, for example, lein new app testapp
( lein
is command line interface for leiningen
a widely used clojure projects automation tool) you may see in your core.clj a line 例如,如果您创建
lein new app testapp
( lein
是leiningen
广泛使用的clojure项目自动化工具的命令行界面),您可能会在core.clj中看到一行
(ns testapp.core
(:gen-class))
(defn -main
"Comment line"
[&args]
(println "Hello, World!"))
Here ns
is a macro that makes easier to define namespaces and other things. ns
是一个宏,可以更轻松地定义名称空间和其他内容。 Important thing here is (:gen-class)
. 这里重要的是
(:gen-class)
。 Let unwrap the macro: 让我们解开宏:
> (macroexpand-1 '(ns testapp.core (:gen-class)))
(do
....
(clojure.core/gen-class :name "testapp.core" :impl-ns testapp.core :main true)
....)
This code will tell the compiler to generate the byte code for the class named "testapp.core" and this class marked as ":main true". 该代码将告诉编译器为名为“ testapp.core”的类生成字节代码,并将该类标记为“:main true”。 This mark will create a declaration of static public
main
function in the class, so jvm
could start a program calling the main
function. 该标记将在类中创建静态公共
main
函数的声明,因此jvm
可以启动一个调用main
函数的程序。 By java language convention the program entry point should be called main
. 按照Java语言约定,程序入口应称为
main
。 You can add different methods to gen-class
(see docs) declaring types or arguments and return value, but, as I can assume, it can be useful to separate java
statically typed methods from your clojure
functions to prevent name collapse. 您可以向
gen-class
(请参阅文档)添加不同的方法来声明类型或参数并返回值,但是,正如我可以假设的那样,将java
静态类型的方法与clojure
函数分开以防止名称崩溃是很有用的。 So gen-class
allows you to define a :prefix
that you use to name your java class implementation. 因此,
gen-class
允许您定义一个:prefix
,用于命名Java类实现。 Default one is "-". 默认值是“-”。 So you create a function
-main
and in java
code it would look like testapp.core.main
. 因此,您创建了一个功能
-main
并在java
代码中看起来像testapp.core.main
。
We can change it 我们可以改变它
(ns testapp.core
(:gen-class :prefix my-prefix-))
When you compile your code to jar and run with java -jar
you get 当您将代码编译为jar并使用
java -jar
运行时,会得到
Exception in thread "main" java.lang.UnsupportedOperationException:
testapp.core/my-prefix-main not defined
Ok, we forgot to rename our implementation: 好的,我们忘记了重命名我们的实现:
(defn my-prefix-main [&args] (println "Hello, World!"))
Compile and run: 编译并运行:
"Hello, World!"
PS I can't tell you about naming conventions of protocols and interfaces that are used in clojure
. PS:我无法告诉您有关
clojure
中使用的协议和接口的命名约定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.