Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is :some-parameter in Clojure?

Tags:

clojure

I'm learning Clojure and I have seen pieces of code with :some-value. What's that ? I saw some code like this

(defn relay [x i]
  (when (:next x)
    (send (:next x) relay i))
  (when (and (zero? i) (:report-queue x))
    (.put (:report-queue x) i))
  x)

if I print the when documentation, I don't find :next there

(doc when)
-------------------------
clojure.core/when
([test & body])
Macro
  Evaluates test. If logical true, evaluates body in an implicit do.
nil

Where is :next definition ?

Thanks!

like image 249
julien nascimento Avatar asked Jan 30 '26 17:01

julien nascimento


1 Answers

Those are keywords. Googling what they are can be surprisingly difficult if you don't already know what they are.

They evaluate to themselves:

user=> :foo ; evaluates to :foo

They are unique, potentially namespace-qualified identifiers. Which is why they are frequently used as keys in maps:

(def stuff {:a 1
            :b 2})

They know how to look themselves up (i.e. you can call them as functions):

(:a stuff) ; 1

...which is the use-case in your example code. They're quite nice.

like image 74
Jared Smith Avatar answered Feb 03 '26 08:02

Jared Smith



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!