A discussion is going on in the #clojure channel on Freenode just now on this very topic. Chris Houser (who was going to post an answer, but ultimately decided he was too busy to do it) has posted a Gist which demonstrates what happens with a boolean
vs. Object
overloaded method; it turns out that in some scenarios, in addition to a (boolean ...)
cast, a type hint is required. The discussion was quite enlightening, with a few dark corners of Clojure compilation process becoming nicely illuminated. (See links to IRC log below.)
Basically, if an object is created right in the method-calling form -- (.foo (Foo.) ...)
, say -- that type hint is not necessary; it is likewise not necessary if the object has been constructed as a value for a local in an enclosing let
form (see update 2 below and my version of the Gist). If the object is obtained by Var lookup, though, a type hint is required -- which can be provided either on the Var itself or, at the call site, on the symbol used to refer to the Var.
The Java code from the Gist:
package mypkg;
public class Ugly {
public Ugly(){}
public String foo(boolean i) { return "bool: " + i; }
public String foo(Object o) { return "obj: " + o; }
}
And the Clojure code:
(.foo (mypkg.Ugly.) 5)
;=> "obj: 5"
(.foo (mypkg.Ugly.) true)
;=> "obj: true"
(.foo (mypkg.Ugly.) (boolean true))
;=> "bool: true"
(def u (mypkg.Ugly.))
(.foo u (boolean true))
;=> "obj: true"
(.foo #^mypkg.Ugly u (boolean true))
;=> "bool: true"
Note how the Clojure compiler needs a type hint on u
to be able to compile a direct method call. Otherwise reflection-based code seems to be generated, which apparently loses track of the fact that the argument is meant to be a primitive along the way.
My additions follow (and here's my fork of the above Gist).
;; renamed mypkg.Ugly to foo.TestInterop2 when doing my tests
user> (let [t (foo.TestInterop2.)]
(.foo t (boolean true)))
"bool: true"
;;; type-hinting the Var
user> (def #^foo.TestInterop2 x (foo.TestInterop2.))
#'user/x
user> (.foo x (boolean true))
"bool: true"
The topic was first brought up at this point. Chouser posted the Gist half an hour later, with the discussion becoming more and more interesting after that.