How does one type-hint an argument to a function call when the argument being passed is the literal nil
? I ran into a situation where I'm trying to proxy a Java class with multiple single-argument constructors, e.g.:
public class MyClass {
public MyClass(String a) {...}
public MyClass(List b) {...}
}
I want to call the constructor that takes a string argument for my proxy, but pass it a null value for that argument. Simply calling:
(proxy [MyClass] [nil] ...)
throws an exception because Clojure finds multiple constructors and doesn't know which one to call. But trying to add a type hint, like so:
(proxy [MyClass] [^String nil] ...)
throws an exception with message "Metadata can only be applied to IMetas". Fair enough - when reading the nil
literal, there's nothing for the reader to attach the metadata to. So how can I work around this? I was able to come up with a solution involving let
, but it seems rather kludgy:
(let [NIL! nil]
(proxy [MyClass] [^String NIL!] ...))
Is there a better way to do this?
Sadly no,
I don't know of any better way. Sometimes I make the proxy
call in a function and then call it, though this is equivalent to your use of let
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With