Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dot-dash vs dot-prefix in Clojure

Tags:

clojure

Why is the dot-dash preferred over dot-prefix when accessing the field of a defrecord in Clojure?

Ex:-

(defrecord Person [name age])

(def person1 (Person. "Ak" 28))

(.-name person1)
"Ak"

(.name person1)
"Ak"
like image 321
Akash jain Avatar asked Oct 18 '25 07:10

Akash jain


1 Answers

.- is used to signify field access in Clojure interop, just . is calling a method.

Either way, as mentioned in a comment on the question, using the keyword as in :name is the idiomatic way to access the name field.

The other methods work because records are built partially to support interop with the host language.


Keep in mind that for data modeling in general with Clojure, maps are preferred. Here's a handy flowchart for choosing the right datatype for your situation.

like image 59
Psetmaj Avatar answered Oct 20 '25 22:10

Psetmaj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!