开发者

Type hinting return value with ^ or :tag meta?

开发者 https://www.devze.com 2023-03-15 07:00 出处:网络
The two seem to be doing the same thing in Clojure. Which syntax is canonical? (defn a ^int [] 4) (defn b ^{:tag int} [] 4)

The two seem to be doing the same thing in Clojure. Which syntax is canonical?

(defn a ^int [] 4)

(defn b ^{:tag int} [] 4)

I hope it's a 开发者_StackOverflowsince it's shorter.


I only use b when I need to include metadata other than just the tag. For example, when implementing a transient collection, I needed

(deftype Whatever [^{:tag ITransientVector
                     :unsynchronized-mutable true} foo])

Note that it would be equivalent and perfectly acceptable to write

(deftype Whatever [^:unsynchronized-mutable ^ITransientVector foo])

but I personally prefer the explicit map.


Its 'a' that is canonical. Its the only one I have seen so far in Clojure code and in the documentation on the Clojure site. See here for more information on type hints.

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号