何時何地在Clojure程式碼中輸入註釋?
何時何地在Clojure程式碼中輸入註釋?顯然,當效能計數.但是有沒有什麼規則可以在何時(僅當做Java Interop?)和哪裡新增它們(函式定義引數)?
我所知道的最重要的原因是效能,在clojure中的型別暗示消除了反射,這可能會降低效能,所以我將型別提示放在我所測量的功能中,使其效能至關重要.
在ofollow,noindex" target="_blank">clojure.org 的java互操作部分有一個description .在這些評論中:
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. …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. Note that type hints are not needed for static members (or their return values!) as the compiler always has the type for statics.
您可以隨時開啟反射標誌的警告,以檢視正在呼叫反射的位置,因此,型別提示可能會有所幫助.
編輯:
關於你的問題“只為java互操作?” – 沒有.即使在純粹的clojure應用程式中,型別提示也很有用.反思放慢效能的問題是JVM上的一個生命的事實,是動態語言一般要處理的問題.
關於何種型別的提示,可以從clojure.org:
They can be placed on function parameters, let-bound names, var names (when defined), and expressions:
http://stackoverflow.com/questions/2569069/when-and-where-do-you-put-type-annotations-in-clojure-code