Fork me on GitHub
#clojure
<
2024-01-12
>
hifumi12307:01:54

Suppose I have a function

(defn  make-thing ^SomeObject [] ...)
then I use it like so
(let [thing (make-thing)]
  ;; assume someMethod exists on SomeObject instances
  (.someMethod thing))
is thing type-hinted with SomeObject? I am writing code interfacing with Webauthn4J and the answer seems to be “no”. If the answer is “yes”, then am I hitting some strange bug with the Clojure compiler?

phronmophobic07:01:56

I think that should work, but the way to check is to (set! *warn-on-reflection* true)

phronmophobic07:01:13

your example seems to not use reflection for me:

(defn  make-thing ^String []
  "")

(let [s (make-thing)]
  (.length s))

hifumi12307:01:00

I guess tehres something with the java library I am using then, because one of my function return type hints are being reported as unknown

hifumi12307:01:13

cant provide a minimal repro yet since this is pretty complicated code and for now im ignoring the warning

phronmophobic07:01:19

does the method have other arguments? It can sometimes get a little hairy if a method has multiple overloads. In some cases, the compiler can't disambiguate and it will fall back to reflection.

lassemaatta07:01:21

also double check that you're hinting with the correct class/interface; I've wasted a lot of time after accidentally importing a similar class from a different package 😅

1
p-himik09:01:22

> one of my function return type hints are being reported as unknown Reported by what?

Jakub Holý (HolyJak)09:01:04

Hello! When writing EDN in the particular case where the input is a data structure (and not e.g. just a string), why should I use pr-str instead of str ? I.e. what does str in this case print that does not round-trip well? Tagged literals for sure. Anything else? Thank you! 🧵

Jakub Holý (HolyJak)09:01:18

Tagged lit. ex.:

(str (t/now))
=> "2024-01-12T09:53:33.027Z"
(pr-str (t/now))
=> "#clj-time/date-time \"2024-01-12T09:53:39.246Z\""

p-himik10:01:19

Anything that overrides .toString.

(str (java.util.HashMap. {1 2 3 4}))
=> "{1=2, 3=4}"
(pr-str (java.util.HashMap. {1 2 3 4}))
=> "{1 2, 3 4}"

p-himik10:01:53

And even when there's no override and no custom tag, the results are different:

(pr-str (Object.))
=> "#object[java.lang.Object 0x6990cbde \"java.lang.Object@6990cbde\"]"
(str (Object.))
=> "java.lang.Object@7d05c924"