I want to call an overloaded Java method from Clojure, and passing in null
(or nil
in Clojure) has special meaning. How would I ensure that the correct Java method is called if the argument is nil
?
e.g. suppose the overloaded Java methods were as follows:
public class Foo {
public String bar(String s) {
return (s == null) ? "default" : s;
}
public String bar(Integer i) {
return (i == null) ? "0" : i.toString();
}
}
I want to yield the value "default"
, so I want to do the following in my Clojure code:
(.bar (Foo.) (cast String nil))
I've tested this in two slightly different environments and I do not get consistent results. And it appears that casting a nil
value in Clojure does not yield the expected type, e.g. --
(type (cast String nil))
; => nil
new Foo().bar((String) null);
which ensures the correct overloaded method is called (i.e. the one withString
in its method signature). - pestrella