简体   繁体   中英

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]

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 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.

You should also check out:

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


You may also wish to review this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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