Strange unexpected defrecord behavior: a bug or a feature?

clojure

Solution

You need to re-define the `some-function` after redefining the record again. The reason for this is that `defrecord` creates a new type (using deftype) and using the `(SomeRecord.)` notation inside the function will bind the code to that type even after a new type with same name is defined. This is why it is usually prefer to use `(->SomeRecord)` notation to instantiate the record, using this notation will make your code work like you expected.

Problem

``` ;; Once upon a time I opened a REPL and wrote a protocol ;; definition: (defprotocol SomeProtocol (f [this])) ;; And a record: (defrecord SomeRecord [] SomeProtocol (f [this] "I don't do a whole lot.")) ;; And a very useful side-effect free function! (defn some-function [] (f (SomeRecord.))) ;; I call my function... (some-function) ;; ...to see exactly what I expect: ;; user=> "I don't do a whole lot." ;; Unsatisfied with the result, I tweak my record a little bit: (defrecord SomeRecord [] SomeProtocol (f [this] "I do a hell of a lot!")) (some-function) ;; user=> "I don't do a whole lot." ``` Looks like a bug to me. I just cannot be sure after having seen so many false compiler bug reports in c++ user group.

Original source