Fork me on GitHub
#component
<
2022-04-26
>
Serafeim Papastefanos22:04:43

Hello can you explain what's the value of connection that's tested in the if in this example

(defrecord IdempotentDatabaseExample [host port connection]
  component/Lifecycle
  (start [this]
    (if connection  ; already started
      this
      (assoc this :connection (connect host port))))
  (stop [this]
    (if (not connection)  ; already stopped
      this
      (do (.close connection)
          (assoc this :connection nil)))))
it's from the readme of the project

hiredman22:04:41

it refers to the record field connection, and the idea there is you create an instance of the defrecord without specifying a value for :connection, so it is nil

hiredman22:04:14

this is the instance of the defrecord, and it assocs in :connection when starting if there is no connection

Serafeim Papastefanos22:04:24

So this is like checking if this.connection is not nil?

Serafeim Papastefanos22:04:09

Thanks! Is there somewhere that this is explained? I read the docs for defrecord but couldn't understand :/

seancorfield22:04:03

Clojure's docstrings are nearly all very brief and tend to assume you already know some basics about the language -- are you learning from any books, as you go?

seancorfield22:04:05

(many people recommend Getting Clojure and/or Programming Clojure, perhaps followed by Clojure Applied -- you should find records are well-covered in books like those)

Serafeim Papastefanos22:04:42

Thank you for the recommendation