Search code examples
clojureread-eval-print-loopinstanceof

What happens when reloading records in Clojure's REPL?


I just got done with a marathon debugging session that I eventually managed to reduce to the following code.

(ns test)

(defrecord Person [name])

(comment

  (def person (->Person "Sebastian"))

  (instance? Person person)

)

So if I load this file in the REPL and then evaluate the two comments in succession, the second comment will evaluate to true. But if I then reload the file and again evaluate the second comment I'll receive false until I reevaluate the first comment.
What exactly is going on here?


Solution

  • defrecord dynamically generates compiled bytecode for the class Person.

    If you invoke it twice, it will generate another class with the same name. So you're asking whether the instance of the newer class is an instance of the older class, which is false.