Fork me on GitHub
Dongwook Kim06:11:43

Hi, can I use type hint with record to function’s parameter?

(defrecord Foo [id name])

(defn prn-foo [^Foo f]
  (prn (:id f)))
above this is not working. But I wonder I can do this with another way or custom type(like defrecord or deftype) would not allow to use as type hint for parameter.


What "is not working"? And why do you think you need the type hint?

Dongwook Kim06:11:05

(defrecord Foo [id name])
(defrecord Bar [id name])

(defn prn-foo [^Foo f]
  (prn (:id f)))

  (prn-foo (->Bar 1 "bar")))
I mean “not working” is In this case, I expect to get a warning when I run a rich-comment block. but it’s not. it runs without any warning or error. I think making a parameter object with defrecord can more express whole data structure in function signature than using map.


Clojure isn't a typed language in that way. Records are mostly just maps. Both Foo and Bar have an id member so (:id f) will work for both.


Type hints, for the most part, just affect Java interop to avoid reflection.


Records are optimized maps that can participate in typed-dispatch, such as via protocols. See

Dongwook Kim06:11:55

hmm, I just want to express whole data structure in function signature using defrecord and type hint. I guess this approach is a wrong way then. Should find other way. thx!


The recommended approach is "just use hash maps" -- unless you specifically need typed-dispatch.


Clojure is a dynamically-typed language so you need to roll with that.

Dongwook Kim06:11:58

thanks for your advice, I’ll follow your recommend. 👍


Hello, need to convert data structure from {:name ["A" "B" "C"] :point [1 2 3]} to: [{:name "A" :point 1} {:name "B" :point 2} {:name "C" :point 3}] any ideas on how to do it? i am newbie and a little bit lost myself on this. Thanks


There's many ways to do it, one way is to

(mapv #(hash-map :name %1 :point %2) (:name a) (:point a))

💜 1

I came up with this as follows: We have two "sequential" things, the vectors ["A" "B" "C"] and [1 2 3], and I want to sort of line them up in a way that preserves order. Already this makes me think of using map (or mapv, which returns a vector instead), because it can go over multiple collections at a time. The anonymous function #(hash-map :name %1 :pont %2) gets two arguments at a time, one from each vector, creates the corresponding map, and then each such result is placed in a vector.

🙌 1

thank you very much!! all clear now!

😀 1

There's probably a nice version with interleave


#(zipmap (:name %) (:point %))


How would you use that function to solve this problem?

Imdad Ahmed20:11:52

Hello, is there an emacs extension that provides a more human readable rendering of cider-errors?


can you show a screenshot of what you are seeing? There was a lot of useful stuff in there. It can filter to just project stackframes, only clojure, etc.


also, by their nature Clojure stacktraces endeavor to never lie, and show you exactly where the error happened. At first glance this can seem overwhelming but reading it carefully can be the best advice

Imdad Ahmed23:11:45

Sorry for the rather late reply. Here is a screenshot from cider:

Imdad Ahmed23:11:18

As opposed to a screenshot i took from babashka's output, which i found easier to parse:

Imdad Ahmed23:11:33

I'm trying to find if there are extensions available for emacs that would help getting the information out of that data in cider-error output

Imdad Ahmed23:11:39

No i haven't. Giving it a glance now, it seems to give me ability to change some default behaviours on where the stacktrace is shown (repl vs a new buffer). Also it gives you some key bindings to make it easier to navigate the stacktrace. I think this is certainly helpful, but may be what i'm looking for is someway to render the data from the error output that makes it more readable.