Hey,
Question about definterface.
When using a definterface we have to qualify all classnames, else Clojure just prefixes the classes with java.lang..
Example:
(definterface IDoStuff
(^CompletableFuture stuffAsync [^String input ^Map option]))
=> user.IDoStuff
(deftype StuffImpl []
IDoStuff
(stuffAsync [_ input option]))
Syntax error (ClassNotFoundException) compiling deftype* at (/private/var/folders/8t/5j07pp9n2371mgs0zzl6d8040000gp/T/form-init8973514254085745336.clj:1:1).
java.lang.Map
When using fully qualified class names, everything works:
(definterface IDoStuff
(^java.util.concurrent.CompletableFuture stuffAsync [^String input ^java.util.Map option]))
=> user.IDoStuff
(deftype StuffImpl []
IDoStuff
(stuffAsync [_ input option]))
=> user.StuffImpl
But now the IDE is falsely claiming I can shorten them, but it's wrong.
What would be the best to address it?
(Other then simply writing a plain .java file)It's not wrong - if you import, you can shorten them. But you might need to reload the namespace for the added imports to take effect?
for example (outside the IDE):
$ clj
Clojure 1.12.4
user=> (import [java.util Map] [java.util.concurrent CompletableFuture])
java.util.concurrent.CompletableFuture
user=> (definterface IDoStuff
(^CompletableFuture stuffAsync [^String input ^Map option]))
user.IDoStuff
^^ works fine (but I evaled the import there)and fyi, Clojure does not prefix the class names, rather most of the classes in java.lang are auto-imported by ns
> ^^ works fine (but I evaled the import there)
It breaks when you write the deftype
This is outside the IDE as well, from lein repl:
nREPL server started on port 56319 on host 127.0.0.1 -
REPL-y 0.5.1, nREPL 1.3.0
Clojure 1.12.2
OpenJDK 64-Bit Server VM 17.0.6+10-LTS
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (import [java.util Map] [java.util.concurrent CompletableFuture])
java.util.concurrent.CompletableFuture
user=> (definterface IDoStuff
#_=> (^CompletableFuture stuffAsync [^String input ^Map option]))
user.IDoStuff
user=> (deftype StuffImpl []
#_=> IDoStuff)
Syntax error (ClassNotFoundException) compiling deftype* at (REPL:1:1).
java.lang.Map
user=> (deftype StuffImpl []
#_=> IDoStuff
#_=> (stuffAsync [_ input options]))
Syntax error (ClassNotFoundException) compiling deftype* at (REPL:1:1).
java.lang.Map I see, thanks
I think this is a bug in Clojure, not Cursive :)
😬
looks like same as https://ask.clojure.org/index.php/4260/definterface-seems-not-resolve-imported-classes-type-hints?show=4260#q4260 if you want to up-vote this
Upvoted, thanks!
I'll have someone look at that, certainly was not in my memory banks!
> I think this is a bug in Clojure, not Cursive 🙂 I've very often thought this to be the case, but I'm nearly always wrong 🙂