2

In clojure, I would like to create a record inside a function.

I tried:

(defn foo []
  (defrecord MyRecord [a b])
  (let [b (MyRecord. "1" "2")]))

But it causes an exception:

java.lang.IllegalArgumentException: Unable to resolve classname: MyRecord

Any idea?

1 Answer 1

11

The key points

You should only use defrecord at top level.1

So, if you do need a custom record type, you should define it outside of foo (at some point in your code which gets processed before foo's definition).

Otherwise, you could just use a regular map. In particular, if foo will be creating entities of multiple "types" (at the conceptual level), it probably makes no sense to try to create a record type (a Java class) for each; the natural solution would be to use maps holding a :type key to indicate the sort of entity represented.

Why it doesn't work

The code from the question doesn't compile, because Clojure's compiler resolves class names mentioned as first arguments to new forms at compile time. ((MyRecord. "1" "2") is expanded to (new MyRecord "1" "2") during the macro expansion process.) Here the name MyRecord cannot yet be resolved to the appropriate class, because the latter has not yet been defined (it would be created by the defrecord form after foo was first called).

To get around this, you could do something horrible like

(defn foo []
  (eval '(defrecord MyRecord [x y]))
  (let [b (clojure.lang.Reflector/invokeConstructor
           ;; assuming MyRecord will get created in the user ns:
           (Class/forName "user.MyRecord")
           (into-array Object ["1" "2"]))]
    b))

which causes a kitten to have its finalize method invoked through reflection, resulting in a gruesome death.

As a final remark, the above horrible version would sort of work, but it would also create a new record type under the same name at each invocation. This causes weirdness to ensue. Try the following examples for a flavour:

(defprotocol PFoo (-foo [this]))

(defrecord Foo [x]
  PFoo
  (-foo [this] :foo))

(def f1 (Foo. 1))

(defrecord Foo [x])

(extend-protocol PFoo
  Foo
  (-foo [this] :bar))

(def f2 (Foo. 2))

(defrecord Foo [x])

(def f3 (Foo. 3))

(-foo f1)
; => :foo
(-foo f2)
; => :bar
(-foo f3)
; breaks

;; and of course
(identical? (class f1) (class f2))
; => false
(instance? (class f1) f2)
; => false

At this point, (Class/forName "user.Foo") (assuming again that all this happens in the user namespace) returns the class of f3, of which neither f1 nor f2 is an instance.


1 Macros occasionally might output a defrecord form wrapped in a do along with some other forms; top-level dos are special, though, in that they act as if the forms they wrap were individually processed at top level.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks a lot! The invokeConstructor function that you refer to helped me solve a very challenging riddle: see my 2nd answer of stackoverflow.com/q/9167457/813665
Why does the defrecord part inside foo have to be executed through eval?
Happy to help. I'd have posted the Reflector bit over there earlier had I noticed that question in an SO answering moment -- glad this came up here. About the foo above, I used eval during an experiment to be able to determine the name of the created record dynamically, then simplified it to this version -- I suppose I could have taken out eval then. Once the shape of the defrecord form is statically fixed, eval is indeed unnecessary, although in that case there is also no reason whatsoever not to use a top-level defrecord to gain a sporting chance at producing a sane program.
Using defrecord within a function is pretty much guaranteed to be at least one of "providing no benefit to the surrounding code" and "causing weird and subtle breakage", and most likely both; the only reason to play with it that I can see is to learn about Clojure's implementation details. Otherwise it's just gallows-grade rope... Mostly writing this so that nobody, by any stretch of the imagination, can get the impression that I offered the foo above as a model for production code -- I only posted it for completeness and because I think it's fun to abuse Clojure at the REPL sometimes.
Do you know the reason why Clojure's compiler resolves class names mentioned as first arguments to new forms at compile time? Why does this need to be at compile time?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.