Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-hint a nil literal in Clojure

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?

like image 614
Alex Avatar asked Nov 07 '13 20:11

Alex


1 Answers

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

like image 101
Arthur Ulfeldt Avatar answered Nov 11 '22 07:11

Arthur Ulfeldt