clojure

Clojure - why can I call (Integer/MAX_VALUE) as a function?


An example probably explains it best. Why does the 2nd and 3rd examples work?

Integer/MAX_VALUE
=> 2147483647
(Integer/MAX_VALUE)
=> 2147483647
((((((Integer/MAX_VALUE)))))) ; wtf?
=> 2147483647

(1) ; crashes as expected
Execution error (ClassCastException) at medino-web.handler/eval7997 (form-init17779606850549883231.clj:1).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')

Solution

  • From the source code of Clojure 1.12:

    // Preserving the existing static field bug that replaces a reference in parens with
    // the field itself rather than trying to invoke the value in the field. This is
    // an exception to the uniform Class/member qualification per CLJ-2806 ticket.
    if(fexpr instanceof StaticFieldExpr)
        return fexpr;
    

    So it seems that that behavior is a bug that was deliberately not fixed to avoid breakage to any existing code that might be relying on it.