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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:27:19+00:00 2026-06-10T00:27:19+00:00

I want to switch on the class of a given object in order to

  • 0

I want to switch on the class of a given object in order to encode it.

(defn encoded-msg-for [msg]
  (case (class msg)
    java.lang.Double   (encode-double msg)
    java.lang.String   (encode-str msg)
    java.lang.Long   (encode-int msg)
    java.lang.Boolean  (encode-bool msg)
    clojure.lang.PersistentArrayMap (encode-hash msg)
    clojure.lang.PersistentVector (encode-vec msg)
    nil "~"
  )
 )

When I call (encoded-msg-for {}), it returns No matching clause: class clojure.lang.PersistentArrayMap

What is odd is that putting the cases into a hash-map (with the classes as keys and strings as values) works perfectly well.

Also, (= (class {}) clojure.lang.PersistentArrayMap) is true. What comparison is happening here and how can I switch either on the class of the object itself or (better) something in its hierarchy?

  • 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-10T00:27:21+00:00Added an answer on June 10, 2026 at 12:27 am

    I believe case treats the class names as literal symbols – it does not resolve them to actual classes:

    >>> (case 'clojure.lang.PersistentArrayMap clojure.lang.PersistentArrayMap 1 17)
    1
    
    >>> (case clojure.lang.PersistentArrayMap clojure.lang.PersistentArrayMap 1 17)
    17
    

    This is rather unintuitive, but so it works in Clojure’s case. Anyway, the idiomatic way is to use defmulti and defmethod instead of switching on type:

    (defmulti encoded-msg class)
    
    (defmethod encoded-msg java.util.Map [x] 5)
    
    (defmethod encoded-msg java.lang.Double [x] 7)
    
    >>> (encoded-msg {})
    5
    
    >>> (encoded-msg 2.0)
    7
    

    The dispatcher uses the isa? predicate which deals well with the comparisons of types, in particular it works well with Java inheritance.

    If you don’t want to use defmulti, then condp might replace case in your use case, as it properly evaluates the test-expressions. On the other hand it doesn’t provide constant time dispatch.

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

Sidebar

Related Questions

I want to switch a object in a viewModel with an other, with the
I want to build a method which accepts a string param, and an object
I have a global settings class, and i want to switch a var (to
I want switch Location Providers On and Off, that is GPS and Wireless Location
I want to switch a file upload from FTP to SFTP. But I cannot
I want to switch images on .click() using the url attr I can do
I want to switch my views in MVC 3 between two languages - PL
I want to Switch from Web Designer to Front End Developer or web developer
I want to switch over to Mono from .Net (plus Visual Studio to whatever
I'm on OSX and want to switch back to my original zsh config from

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.