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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:12:09+00:00 2026-06-09T22:12:09+00:00

I was playing around in the REPL and I got some weird behavior: Clojure

  • 0

I was playing around in the REPL and I got some weird behavior:

Clojure 1.4.0
user=> (type {:a 1})
clojure.lang.PersistentArrayMap
user=> (def x {:a 1})
#'user/x
user=> (type x)
clojure.lang.PersistentHashMap

I thought that all small literal maps were instances of PersistentArrayMap, but apparently that’s not the case if it’s been bound with def. Why would using def cause Clojure to choose a different representation for my litte map? I know it’s probably just some strange implementation detail, but I’m curious.

  • 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-06-09T22:12:11+00:00Added an answer on June 9, 2026 at 10:12 pm

    This question made me dig into the Clojure source code. I just spent a few hours putting print statements in the source in order to figure this out.

    It turns out the two map expressions are evaluated through different code paths

    (type {:a 1}) causes Java byte-code to be emitted and ran. The emitted code use clojure.lang.RT.map() to construct the map which returns a PersistentArrayMap for small maps:

    static public IPersistentMap map(Object... init){
        if(init == null)
            return PersistentArrayMap.EMPTY;
        else if(init.length <= PersistentArrayMap.HASHTABLE_THRESHOLD)
            return PersistentArrayMap.createWithCheck(init);
        return PersistentHashMap.createWithCheck(init);
    }
    

    When evaluating (def x {:a 1}) at least from the REPL there’s no byte-code emitted. The constant map is parsed as a PersistentHashMap in clojure.lang.Compiler$MapExpr.parse() which returns it warpped it in a ConstantExpr:

    else if(constant)
    {
    IPersistentMap m = PersistentHashMap.EMPTY;
    for(int i=0;i<keyvals.length();i+= 2)
        {
        m = m.assoc(((LiteralExpr)keyvals.nth(i)).val(), ((LiteralExpr)keyvals.nth(i+1)).val());
        }
    //System.err.println("Constant: " + m);
    return new ConstantExpr(m);
    }
    

    The def expression when evaluated binds the value of the ConstantExpr created above which as as said is a PersistentHashMap.

    So why is it implemented this way?

    I don’t know. It could be simple oversight or the PersistentArrayMap optimization may not really be worth it.

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

Sidebar

Related Questions

I have been playing around with Node's REPL. I thought it would be pretty
I'm playing around with Clojure, and I can't figure out how to import a
Just playing around with some of the APIs in .NET and I can't seem
I've been playing around with creating a new user and I keep getting this
I'm just playing around with some code but it seemed to have the specific
Playing around with Flask and just wanted to print out some data as JSON
Playing around with Google Maps these days, with some directions. I have a map
After playing around with URL decoding myself, I managed to come up with some
After playing around with some toy applications, exploring the documentation and googling around (including
Playing around with type-classes I came up with the seemingly innocent class Pair p

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.