在Clojure函数参数定义中使用^ [英] Use of ^ in clojure function parameter definition

查看:80
本文介绍了在Clojure函数参数定义中使用^的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这行是什么意思? -> [^ java.io.BufferedReader阅读器]

what does this line mean? -> [^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


Clojure支持使用类型提示来帮助编译器避免在性能关键的代码区域中进行反射。通常,除非存在已知的性能瓶颈,否则应避免使用类型提示。类型提示是放在编译器使用的符号或表达式上的元数据标签。可以将它们放在函数参数,让定名称,变量名称(定义时)和表达式上:

Clojure supports the use of type hints to assist the compiler in avoiding reflection in performance-critical areas of code. 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.

您还应该签出:

  • https://clojure.org/guides/weird_characters
  • https://clojure.org/reference/reader

而且永远不会一直打开浏览器选项卡以 Clojure秘籍

And never, ever fail to keep open a browser tab to The Clojure CheatSheet

您可能还希望查看此答案

这篇关于在Clojure函数参数定义中使用^的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆