Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection warning despite type hint to Java constructor in Clojure

The following code gives me a reflection warning in spite of the type hint.

(set! *warn-on-reflection* true)

(IllegalArgumentException.
  ^String (with-out-str (print "hi")))

The warning:

Reflection warning ... 
call to java.lang.IllegalArgumentException ctor 
can't be resolved.

The code has been extracted and simplified from a more complex example where pretty printing an arbitrary object is performed within the with-out-str. I am using Clojure 1.10.0.

like image 840
chris Avatar asked Nov 26 '25 18:11

chris


1 Answers

This is CLJ-865. It's not specific to with-out-str: adding a type hint to any form which is a macro invocation will typically discard it. The typical workaround is the one in your answer: define a local saving the value, artificially introducing an non-macro form to annotate.

like image 83
amalloy Avatar answered Dec 01 '25 05:12

amalloy