简体   繁体   English

在clojure函数名中使用 - 和*

[英]Use of - and * in clojure function names

Every so often I run into a bit of clojure code where a function name either begins with "-" or ends with "*" and while I can make some guesses from the context, I haven't been able to find any exact definitions of what they indicate. 我经常遇到一些clojure代码,其中函数名称以“ - ”开头或以“*”结尾,虽然我可以从上下文中做出一些猜测,但我还是找不到任何确切的定义。他们表示什么。 Could someone give me a quick explanation or point me somewhere that has one? 有人可以给我一个快速的解释或指向我有一个?

Conventions for functions are...conventions. 函数约定是......约定。 There no mandatory naming but some habits whom sometime came from lisps. 没有强制命名,但有些习惯有时来自lisps。

  • -function is used by some programmers for private functions declared by defn-. 某些程序员使用函数来定义defn-声明的私有函数。 But also 但是也
  • .-function for attribute access for objects from ClojureScript. .-来自ClojureScript的对象的属性访问功能。
  • function? 功能? are generally boolean test function (even?, list?, ...) 通常是布尔测试函数(偶数?,列表?,...)
  • function! 功能! are functions that involve side effect. 是涉及副作用的功能。
  • function name embedding -> are conversion functions. 函数名嵌入 - >是转换函数。
  • *var* are dynamic variables. * var *是动态变量。

And a lot more from programming group conventions. 编程组约定还有很多。

And the last part: foo* is often used for the underlying function of a convenience macro foo . 最后一部分: foo*经常用于方便宏foo的底层函数。 Eg, log vs log* 例如, log vs log*

"-" and "*" are just part of a valid Clojure symbol. “ - ”和“*”只是有效Clojure符号的一部分。 They do not indicate anything special per se, and the Clojure compiler will treat them just like any other symbol. 它们本身没有任何特殊的含义,而Clojure编译器会像对待任何其他符号一样对待它们。

Conventionally, *'s are used to surround the name of a dynamic var. 通常,*用于包围动态var的名称。 You can make a var dynamic without the *'s, but it might cause confusion for someone dealing with your code when the value of the var unexpectedly changes out from under them due to a binding call somewhere up the stack. 你可以在没有*的情况下创建一个var动态,但是当var的值由于binding调用在堆栈的某个地方而从其下面意外地改变时,它可能会导致处理代码的人混淆。 By including the *'s, you are reminding any users of that var that it is dynamic everytime they write/read its name. 通过包含*,您提醒该var的每个用户每次写/读其名称时都是动态的。

Symbols that begin with "-" are typically used in connection with Clojure's gen-class construct. 以“ - ”开头的符号通常与Clojure的gen-class结构一起使用。 That is because "-" is the default prefix to append to method names when mapping a class's method to a function. 这是因为“ - ”是在将类的方法映射到函数时附加到方法名称的默认前缀。 However, there are other things it could mean as well. 但是,它还有其他意义。

As Ivan points out, there are other conventions that might be worth knowing as well. 正如伊万指出的那样,还有其他一些惯例也值得了解。 However, they're just conventions -- like camelCasing in other languages. 但是,它们只是惯例 - 就像其他语言中的camelCasing一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM