Type - hover a null literal on Clojure

How is one type to call an argument to a function call when the argument passed is a literal nil ? I came across a situation when I try to proxy a Java class with several constructors with one argument, for example:

 public class MyClass { public MyClass(String a) {...} public MyClass(List b) {...} } 

I want to call a constructor that takes a string argument for my proxy, but passes it a null value for this argument. Just by calling:

 (proxy [MyClass] [nil] ...) 

throws an exception because Clojure finds several constructors and does not know which one to call. But when trying to add a type hint, for example:

 (proxy [MyClass] [^String nil] ...) 

throws an exception with the message "Metadata can only be applied to IMetas." Fair enough - when reading the nil literal, nothing is needed to read the metadata. So how can I get around this? I was able to come up with a solution that included let , but it seems to be kludgy:

 (let [NIL! nil] (proxy [MyClass] [^String NIL!] ...)) 

Is there a better way to do this?

+6
source share
1 answer

Unfortunately not,

I do not know a better way. Sometimes I call a proxy call in a function and then call it, although this is equivalent to using let

+1
source

Source: https://habr.com/ru/post/957623/


All Articles