clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
Phil Shapiro 2020-12-25T15:23:58.450900Z

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=> 

2020-12-25T15:42:32.452Z

I haven't discovered a way to do it in a few other minor variations using let,either. ^int type hints give errors for several variations of expressions there.

2020-12-25T15:44:36.452500Z

yeah, I tried defining a function in order to type hint the function args, that didn't help either - I was surprised

2020-12-25T15:45:51.453Z

I guess it's possible with some reflection hacking, but that seems a bit much

2020-12-25T15:46:25.453500Z

it should be possible with compile-time only reflection though(?)

2020-12-25T15:51:40.454400Z

@pshapiro try this:

(.remove ^LinkedList ll (int 0))

2020-12-25T15:54:38.456300Z

I ran your code with

(set! *warn-on-reflection* true)
which said
call to method remove can't be resolved (target class is unknown)
this is solved with hinting the target class which is
ll

👍 1
1
Phil Shapiro 2020-12-25T16:04:58.456800Z

Thanks, that’s exactly what I was looking for!

Tamas 2020-12-25T16:33:18.460500Z

for the 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 int ie. (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 ll. Reference: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L1538