ClojureDocs

Nav

Namespaces

Clojure/Conj 2026 — Charlotte, NC — Sept 30 - Oct 2 Learn More & Get Tickets →

*warn-on-reflection*

clojure.core

Available since 1.0Clojure/JVMClojureScriptbabashka
    When set to true, the compiler will emit warnings when reflection is
    needed to resolve Java method calls or field accesses.
     Defaults to false.
    3 Examples
    ;; define two variables then set *warn-on-reflection* to true and try to call
    ;; one of their Java methods. Warnings are generated in both cases
    ;; set *warn-on-reflection* to false and note that you can call both functions
    ;; without a warning
    
    user=> (def i 23)
    #'user/i
    user=> (def s "123")
    #'user/s
    user=> (set! *warn-on-reflection* true)
    true
    user=> (.toString i)
    Reflection warning, NO_SOURCE_PATH:4 - reference to field toString can't be resolved.
    "23"
    user=> (.toString s)
    Reflection warning, NO_SOURCE_PATH:5 - reference to field toString can't be resolved.
    "123"
    
    ;; Reflection (and hence reflection warnings) could be removed by giving type hints:
    user=> (.toString ^String s)
    "123"
    
    user=> (set! *warn-on-reflection* false)
    false
    user=> (.toString i)
    "23"
    user=> (.toString s)
    "123"
    user=>
    (set! *warn-on-reflection* true)  ;; turn on *warn-on-reflection*
    (def s "abc")                     ;; define s
    (.toString s)                     ;; => "abc"; WARN: type of s is unknown
    (.toString ^String s)             ;; => "abc"; no warn given the type hint
    (let [s "abc"] (.toString s))     ;; => "abc"; no warn since Clojure can infer
                                      ;;           type for the local binding
    (set! *warn-on-reflection* false) ;; turn off *warn-on-reflection*
    
    ;; Important to note *warn-on-reflection* take its effect at compile time,
    ;; so binding the option won't work... even with a set! inside.
    ;;   - binding creates a new env to evaluate subsequent statements
    ;;   - yet, compiler still lives in the current env, so it won't see the value
    
    (binding [*warn-on-reflection* true]
      (set! *warn-on-reflection* true)
      (def s "abc")
      (.toString s))                          ;; => "abc", no warning
    (println *warn-on-reflection*)            ;; => false
    
    See Also

    While bound to true, compilations of +, -, *, inc, dec and the coercions will be done without over...

    Added by miner
    0 Notes
    No notes for *warn-on-reflection*