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

  • Home
  • SEARCH
  • 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 3616174
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:28:01+00:00 2026-05-18T22:28:01+00:00

Following up on How to make a record from a sequence of values ,

  • 0

Following up on How to make a record from a sequence of values, how can you write a defrecord constructor call and assign the fields from a Map, leaving un-named fields nil?

(defrecord MyRecord [f1 f2 f3])
(assign-from-map MyRecord {:f1 "Huey" :f2 "Dewey"})  ; returns a new MyRecord

I imagine a macro could be written to do this.

  • 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-18T22:28:02+00:00Added an answer on May 18, 2026 at 10:28 pm

    You can simply merge the map into a record initialised with nils:

    (merge (MyRecord. nil nil nil) {:f1 "Huey" :f2 "Dewey"})
    

    Note that records are capable of holding values stored under extra keys in a map-like fashion.

    The list of a record’s fields can be obtained using reflection:

    (defn static? [field]
      (java.lang.reflect.Modifier/isStatic
       (.getModifiers field)))
    
    (defn get-record-field-names [record]
      (->> record
           .getDeclaredFields
           (remove static?)
           (map #(.getName %))
           (remove #{"__meta" "__extmap"})))
    

    The latter function returns a seq of strings:

    user> (get-record-field-names MyRecord)
    ("f1" "f2" "f3")
    

    __meta and __extmap are the fields used by Clojure records to hold metadata and to support the map functionality, respectively.

    You could write something like

    (defmacro empty-record [record]
      (let [klass (Class/forName (name record))
            field-count (count (get-record-field-names klass))]
        `(new ~klass ~@(repeat field-count nil))))
    

    and use it to create empty instances of record classes like so:

    user> (empty-record user.MyRecord)
    #:user.MyRecord{:f1 nil, :f2 nil, :f3 nil}
    

    The fully qualified name is essential here. It’s going to work as long as the record class has been declared by the time any empty-record forms referring to it are compiled.

    If empty-record was written as a function instead, one could have it expect an actual class as an argument (avoiding the “fully qualified” problem — you could name your class in whichever way is convenient in a given context), though at the cost of doing the reflection at runtime.

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

Sidebar

Related Questions

Can someone help me figure out the following make file? BINS=file1 file2 file3 all:
I make the following call to the DB. @patientRegistration = PatientRegistration.find(:all, :conditions=>[name = '#{patientName}'])
If I have the following: {hdrs: [Make,Model,Year], data : [ {Make:Honda,Model:Accord,Year:2008} {Make:Toyota,Model:Corolla,Year:2008} {Make:Honda,Model:Pilot,Year:2008}] }
I use functions like the following to make temporary tables out of crosstabs queries.
Code that is untestable really annoys me. The following things make oo-code untestable: global
When using IF statements in Python, you have to do the following to make
i'm trying to make the following routes .. and currently i'm going about this
How do I make the following url forbidden in apache; main/index.php?site=ing I have tried
I am using the following code to make a HttpWebRequest and render the XML
How do you make the following code work? example = %%(test)% % {'test':'name',} print

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.