Fork me on GitHub
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]
  (start [this]
    (if connection  ; already started
      (assoc this :connection (connect host port))))
  (stop [this]
    (if (not connection)  ; already stopped
      (do (.close connection)
          (assoc this :connection nil)))))
it's from the readme of the project


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


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 :/


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?


(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