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 1073637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:01:02+00:00 2026-05-16T21:01:02+00:00

In Clojure, given a class name as a string, I need to create a

  • 0

In Clojure, given a class name as a string, I need to create a new instance of the class. In other words, how would I implement new-instance-from-class-name in

(def my-class-name "org.myorg.pkg.Foo")
; calls constructor of org.myorg.pkg.Foo with arguments 1, 2 and 3
(new-instance-from-class-name  my-class-name 1 2 3) 

I am looking for a solution more elegant than

  • calling the Java newInstance method on a constructor from the class
  • using eval, load-string, …

In practice, I will be using it on classes created using defrecord. So if there is any special syntax for that scenario, I would be quite interested.

  • 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-16T21:01:02+00:00Added an answer on May 16, 2026 at 9:01 pm

    There are two good ways to do this. Which is best depends on the specific circumstance.

    The first is reflection:

    (clojure.lang.Reflector/invokeConstructor
      (resolve (symbol "Integer"))
      (to-array ["16"]))
    

    That’s like calling (new Integer "16") …include any other ctor arguments you need in the to-array vector. This is easy, but slower at runtime than using new with sufficient type hints.

    The second option is as fast as possible, but a bit more complicated, and uses eval:

    (defn make-factory [classname & types]
      (let [args (map #(with-meta (symbol (str "x" %2)) {:tag %1}) types (range))]
        (eval `(fn [~@args] (new ~(symbol classname) ~@args)))))
    
    (def int-factory (make-factory "Integer" 'String))
    
    (int-factory "42")
    

    The key point is to eval code that defines an anonymous function, as make-factory does. This is slow — slower than the reflection example above, so only do it as infrequently as possible such as once per class. But having done that you have a regular Clojure function that you can store somewhere, in a var like int-factory in this example, or in a hash-map or vector depending on how you’ll be using it. Regardless, this factory function will run at full compiled speed, can be inlined by HotSpot, etc. and will always run much faster than the reflection example.

    When you’re specifically dealing with classes generated by deftype or defrecord, you can skip the type list since those classes always have exactly two ctors each with different arities. This allows something like:

    (defn record-factory [recordname]
      (let [recordclass ^Class (resolve (symbol recordname))
            max-arg-count (apply max (map #(count (.getParameterTypes %))
                                          (.getConstructors recordclass)))
            args (map #(symbol (str "x" %)) (range (- max-arg-count 2)))]
        (eval `(fn [~@args] (new ~(symbol recordname) ~@args)))))
    
    
    (defrecord ExampleRecord [a b c])
    
    (def example-record-factory (record-factory "ExampleRecord"))
    
    (example-record-factory "F." "Scott" 'Fitzgerald)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm calling Clojure from Java and calling eval on a string passed in. The
Im a clojure beginner.I did the following steps as given on http://www.unexpected-vortices.com/clojure/brief-beginners-guide/development-env.html to setup
Given that it's impossible to see into the future, what factors related to Clojure,
Clojure structs can be arbitrarily extended, adding new fields. Is it possible to extend
Context Clojure Agents are NOT sent new values. They are sent a function which
With Clojure (and other Lisp dialects) you can modify running code. So, when a
I have this simple graph: name -> string ^ | v label let matrix
Given a java package x.y.z, can I alias x.y.z to a shorter name, so
Given a function object or name, how can I determine its arity? Something like
I am a Clojure n00b trying to create some XML strings. My goal is

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.