Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Java Interop by

Maybe there’s a good reason why it doesn’t, but it seems to me it would be great if def would automatically infer type hint:

(let [s "abc"]
  (.chars s)) ;; all fine, no warning

(def s "abc")

(.chars s) ;; Reflection warning, null:21:0 - reference to field chars can't be resolved.

(meta #'s) ;; ↓ notice -- no tag
;; {:line 19,
;;  :column 0,
;;  :file "NO_SOURCE_FILE",
;;  :name s,
;;  :ns #object[clojure.lang.Namespace 0x78068afb "user"]}

Related jira: https://clojure.atlassian.net/browse/CLJ-2851
Related question: https://ask.clojure.org/index.php/9167/infer-hint-from-return-type-hint-function-call-initializes

1 Answer

+1 vote
selected by
Best answer

the last time I recall some discussion about this, I think rhickey rejected it because vars are mutable and type hints effect compilation, so if if you type hint a var as some type X later code will be compiled to depend on it, and won't be recompiled if you change the hinted type (which would be more likely to happen if the hint was automatic). so hints on vars are opt in not opt out.

I see! Thanks