简体   繁体   English

在Clojure函数参数定义中使用^

[英]Use of ^ in clojure function parameter definition

(defn lines
  "Given an open reader, return a lazy sequence of lines"
  [^java.io.BufferedReader reader]
  (take-while identity (repeatedly #(.readLine reader))))

what does this line mean? 这行是什么意思? -> [^java.io.BufferedReader reader] -> [^java.io.BufferedReader reader]

also I know this is a dumb question. 我也知道这是一个愚蠢的问题。 can you show me the documentation where I could read this myself? 您能给我看看我自己可以阅读的文档吗? So that I don't have to ask it here :) 这样我就不必在这里问了:)

You can find documentation here: 您可以在此处找到文档:

https://clojure.org/reference/java_interop#typehints https://clojure.org/reference/java_interop#typehints

Clojure supports the use of type hints to assist the compiler in avoiding reflection in performance-critical areas of code. Clojure支持使用类型提示来帮助编译器避免在性能关键的代码区域中进行反射。 Normally, one should avoid the use of type hints until there is a known performance bottleneck. 通常,除非存在已知的性能瓶颈,否则应避免使用类型提示。 Type hints are metadata tags placed on symbols or expressions that are consumed by the compiler. 类型提示是放在编译器使用的符号或表达式上的元数据标签。 They can be placed on function parameters, let-bound names, var names (when defined), and expressions: 它们可以放在函数参数,让界名称,变量名称(定义时)和表达式上:

 (defn len [x] (.length x)) (defn len2 [^String x] (.length x)) ... 

Once a type hint has been placed on an identifier or expression, the compiler will try to resolve any calls to methods thereupon at compile time. 将类型提示放在标识符或表达式上后,编译器将尝试在编译时解析对方法的任何调用。 In addition, the compiler will track the use of any return values and infer types for their use and so on, so very few hints are needed to get a fully compile-time resolved series of calls. 另外,编译器将跟踪任何返回值的使用以及用于其用途的推断类型,依此类推,因此只需很少的提示即可获得完整的编译时解析的调用。

You should also check out: 您还应该签出:

And never, ever fail to keep open a browser tab to The Clojure CheatSheet 永远不会永远不会打开浏览器选项卡的Clojure CheatSheet


You may also wish to review this answer . 您可能还希望查看此答案

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

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