Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:57:43+00:00 2026-05-29T19:57:43+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:57:44+00:00Added an answer on May 29, 2026 at 7:57 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a clojure function: (defn f [arg1 arg2] ...) I would like to
In clojure, I would like to write a defn-my macro that creates a function
Is there a pretty printing function in Clojure that would output data-structures like lists
I would like to create a factory, in clojure where the number of arguments
I would like to create/drop a database from clojure.java.jdbc. This fails: (require '[clojure.java.jdbc :as
I would like to cast a clojure Java object (assigned with let*) to another
I would like to move data back and fourth between clojure applications. Application settings
What I would like to do (in Clojure): For example, I have a vector
In Clojure, I have a collection coll of 2-elements vectors. I would like to
I'm learning Clojure and would like some advice on idiomatic usage. As part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.