This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
I have a question about java interop with overloaded methods. I want to call
LinkedList.remove(int) from clojure and I can’t seem to do it. I can only call
LinkedList.remove(Object). I’ve read the Clojure docs on java interop and a few stackoverflow pages and haven’t found the solution yet.
$ clojure Clojure 1.10.1 user=> (import (java.util LinkedList)) java.util.LinkedList user=> (def ll (LinkedList.)) #'user/ll user=> (.push ll "a") nil user=> (.push ll "b") nil user=> (.remove ll 0) false user=> (.remove ll (int 0)) false user=> (.remove ll "b") true user=>
I haven't discovered a way to do it in a few other minor variations using
^int type hints give errors for several variations of expressions there.
yeah, I tried defining a function in order to type hint the function args, that didn't help either - I was surprised
I ran your code with
(set! *warn-on-reflection* true)
this is solved with hinting the target class which is
call to method remove can't be resolved (target class is unknown)
LinkedList interop before it is probably better to type hint
ll on the def?
If you do that you wouldn't need the cast to
(remove ll 0) works.
Additionally, if you don't do it then you get the same warning on
(.push ll "a") ie. the Clojure compiler will fail to find the method (at compile time), because it doesn't know the type of