0

在函数定义中:

(defn ^boolean =
  ;;other arities omitted...
  ([x y]
    (if (nil? x)
      (nil? y)
      (or (identical? x y)
        ^boolean (-equiv x y))))

函数定义中的^boolean部分是什么意思?它只是扩展元数据并表示返回类型,还是有更深层次的含义?换句话说,它是否比简单地使代码更具自我描述性更有价值?

4

1 回答 1

2

It is a type hint. See

https://www.safaribooksonline.com/library/view/clojure-programming/9781449310387/ch09s05.html

http://clojure-doc.org/articles/language/functions.html

or your favorite book. PLEASE NOTE: the compiler does not enforce that the actual type matches the type hint! Example w/o type hint:

(defn go []
  "banana" )
(println (go))
;=> banana

(defn ^long go []
  "banana" )
(println (go))
;=> Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number, 
于 2015-09-20T20:26:07.237 回答