interop 2025-07-26

At the moment, I'm trying to remove a reflection warning, namely call to java.util.HashSet ctor can't be resolved.. I'm doing this:

A moment, whilst I get the right clojure...

(->> results
                 (map build-credential-id)
                 (HashSet/new))

I've tried type hinting all over the place, but to no avail (even type hinting the return type of build-credential-id (which is returning a Java class)

The only way I could remove the type hint is this:

(let [hashset (HashSet.)]
              (->> results
                   (map build-credential-id)
                   (.addAll hashset))
              hashset))

Alex Miller (Clojure team) 2025-07-26T20:30:53.736279Z

You could use param-tags on HashSet/new - that makes it explicit

Alex Miller (Clojure team) 2025-07-26T20:31:40.595439Z

(^[Collection] HashSet/new)

Alex Miller (Clojure team) 2025-07-26T20:31:56.175989Z

Not sure if that’s the right type but whatever that should be

Alex Miller (Clojure team) 2025-07-26T20:32:24.235329Z

That will either work without reflection or throw, never reflects

Hmm, you know I did try that, let me try again

(I reviewed the interop page before coming here and tried the Clojure 1.12 new thingie πŸ™‚ )

That seems to make the clojure compiler complain:

(->> results
                 (map build-credential-id)
                 ([^PublicKeyCredentialDescriptor] HashSet/new))

Unmatched delimiter: ]

sorry, my mistake, had the caret in the wrong place πŸ™‚

Ended up with this:

; (err) Syntax error (IllegalArgumentException) compiling HashSet/new at (src/repository.clj:101:18).
; (err) Error - param-tags [com.yubico.webauthn.data.PublicKeyCredentialDescriptor] insufficient to resolve constructor in class java.util.HashSet

(->> results
                 (map build-credential-id)
                 (^[PublicKeyCredentialDescriptor] HashSet/new)))

Oooh, I see, if I put it like this ^[Collection] it works

Wunderbar. Thank you! πŸ™‚ I got there in the end, with a bit of hinting, ta πŸ™‚

Working without reflection warning:

(->> results
                 (map build-credential-id)
                 (^[Collection] HashSet/new)))

but that feels very wrong

Any hints (pun intended! πŸ™‚ )?